laravel-handlebars icon indicating copy to clipboard operation
laravel-handlebars copied to clipboard

How to use this client side?

Open Swanty opened this issue 8 years ago • 7 comments

I'm using laravel 5.2 and raw output doesn't work for me

File: resources/views/test/sample.hbs

<div class="entry">
    <h1>{{title}}</h1>
    <div class="body">{{body}}</div>
</div>

File: resources/views/template/default.blade.php

<head>
...
    <script id="sample-template" type="text/x-handlebars-template">
        @raw('test.sample')
    </script>
...
</head>

Output:

    <script id="sample-template" type="text/x-handlebars-template">
        <div class="entry">
    <h1></h1>
    <div class="body"></div>
</div>
    </script>

Basically {{title}} and {{body}} is gone, so I can't use the template in javascript. I'm using the default config file and laravel-handlebars version 1.1.0

Swanty avatar Aug 09 '16 09:08 Swanty

Decided to just compile the templates instead of injecting them into html.

First I tried to use laravel-elixir-handlebars from npm, but that one adds require() in compiled templates.js file, which doesn't fit my needs, also last update was a year ago and has PR's that the owner hasn't bothered to look at.

Soo I made one that works for me. If anyone is interested then you can check out at https://github.com/Swanty/laravel-elixir-handlebars

Swanty avatar Aug 17 '16 08:08 Swanty

I also had a bit of an issue using - or more likely, understanding - raw. I expected the template to be pulled as is with minimal processing, but when tracing the code I saw it was still being compiled, but in raw mode (though I'm not sure exactly how that differs or how it would be used). In the end, since I didn't have any dependencies on blade, lang, etc in my template, I ended up using a direct php include:

<script id="product-list-template" type="text/x-handlebars-template">
<?php include(base_path('resources/views/product/hogan/product-list-template.hbs')); ?>
</script>

pdbreen avatar Oct 26 '16 16:10 pdbreen

You seem to be correct, raw output is currently broken. I'd be happy to review pull requests, because It may take some time until I'll get around to fix it.

We are using webpack in our pipeline, which is taking care of shipping templates to the front-end, which is why I may not have noticed this problem before.

Also, maybe @markusjwetzel can take a look into it?

Lorti avatar Oct 27 '16 08:10 Lorti

Sorry, don't have time to look into this issue, so help is wanted. I added a hint to the raw output section of the readme file, so hopefully someone will read it in the readme or here and is able to help. :)

markusjwetzel avatar Oct 31 '16 19:10 markusjwetzel

This is how I do it, using basic webpack configuration: https://medium.com/@greut/sharing-templates-between-php-and-javascript-in-laravel-a5e07b43be24

greut avatar Feb 08 '17 10:02 greut

Great article! I added a link to your article in the readme file. 👍

markusjwetzel avatar Feb 08 '17 12:02 markusjwetzel

@greut @markusjwetzel This approach does not work with the helper function {{lang 'message' }}.

marijoo avatar Feb 25 '19 13:02 marijoo