ProAI / laravel-handlebars

:bicyclist: Laravel wrapper of LightnCandy for using Handlebars (and Mustache) templates
MIT License
39 stars 18 forks source link

How to use this client side? #12

Open Swanty opened 8 years ago

Swanty commented 8 years ago

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 commented 8 years ago

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

pdbreen commented 7 years ago

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>
Lorti commented 7 years ago

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?

markusjwetzel commented 7 years ago

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. :)

greut commented 7 years ago

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

markusjwetzel commented 7 years ago

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

marijoo commented 5 years ago

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