proai / laravel-handlebars Goto Github PK
View Code? Open in Web Editor NEW:bicyclist: Laravel wrapper of LightnCandy for using Handlebars (and Mustache) templates
License: MIT License
:bicyclist: Laravel wrapper of LightnCandy for using Handlebars (and Mustache) templates
License: MIT License
Hi Markus,
do you plan on updating this package to use the latest LightnCandy?
I have naively updated the version number in a fork and got some error messages. It seems that there have been some API changes since v0.89?
If you don't have the time I will look further into it and maybe open up a pull request.
Grüße aus Linz,
Manuel
Hi,
I'm quite lost on how to use the package with Laravel 5. I want to use the templates both in server and in client.
hotness
in /resources/views/helpers/hotness.js
.'helpers' => ['hotness'],
to /config/handlebars.php
.{{hotness job.company.hotness }}
on my .hbs templateI have the following error:
ErrorException in lightncandy.php line 792:
Can not find custom helper function defination hotness() ! (View: /home/.../resources/views/job/list.blade.php)
What's wrong? Also, could you share a working example?
Thanks a lot!!
Ok so I realise this is a niche feature most people won't use, but I find especially useful when using a template engine like this.
View composers work fine for the main view or layout of a page but when you register for partial included using the {{> _partialname }} syntax it never fires.
I currently use the view composer for my layout controller which trims that down nicely, and lets me set some variables as arrays that I cannot access directly in handlebars normally. I wanted to implement a view composer for a partial called sidebar since I have a lot of custom stats being pulled in for the sidebar, but currently limited to one view composer.
I got this message when i used customer helper in .hbs file.
I have 3 handlebars template files inside views/templates
directory:
test.hbs
:
<div>
<p>Hello</p>
{{> testPartial}}
</div>
testPartial.hbs
:
<div>
<p>This is inside a partial</p>
{{> testPartial2}}
</div>
testPartial2.hbs
:
<div>
<p>This is inside partial 2</p>
</div>
When I execute return view('templates.test');
, I get:
<div>
<p>Hello</p>
<div>
<p>This is inside a partial</p>
<div>
<p>This is inside partial 2</p>
</div>
</div>
</div>
But I should get:
<div>
<p>Hello</p>
<div>
<p>This is inside a partial</p>
<div>
<p>This is inside partial 2</p>
</div>
</div>
</div>
In case of handlebars with javascript, the indentation works as expected.
handlebars.php
config file:<?php
use ProAI\Handlebars\Support\LightnCandy;
return [
/*
|--------------------------------------------------------------------------
| Flags
|--------------------------------------------------------------------------
|
| Set Lightncandy flags here. See https://github.com/zordius/lightncandy
| for more information.
|
*/
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_HANDLEBARSJS_FULL,
/*
|--------------------------------------------------------------------------
| File Extensions
|--------------------------------------------------------------------------
|
| All file extensions that should be compiled with the Handlebars template
| engine. Unless you specify your own partial resolver the package will
| look for files in Laravel's view storage paths.
|
*/
'fileext' => [
'.handlebars',
'.hbs',
],
/*
|--------------------------------------------------------------------------
| Cache Busting
|--------------------------------------------------------------------------
|
| Using nested Handlebars partials makes is difficult to determine if the
| view at a given path is expired. Therefore you can specify environments
| where the cached views will be recompiled on each request.
|
*/
'development_environment' => [
'local',
],
/*
|--------------------------------------------------------------------------
| Partials
|--------------------------------------------------------------------------
|
| https://github.com/zordius/lightncandy#partial-support
|
*/
'partials' => [],
'partialresolver' => false,
/*
|--------------------------------------------------------------------------
| Helpers
|--------------------------------------------------------------------------
|
| https://github.com/zordius/lightncandy#custom-helper
|
*/
'helpers' => [],
'helperresolver' => false,
/*
|--------------------------------------------------------------------------
| Language Helpers
|--------------------------------------------------------------------------
|
| Use this option, if you want to use the language helpers in a template.
| You can use a {{lang ...}} and {{choice ...}} helper. Both have the same
| behaviour like the @lang and @choice Blade directives.
|
*/
'language_helpers' => true,
/*
|--------------------------------------------------------------------------
| Optional Raw Output
|--------------------------------------------------------------------------
|
| If this option is set to true, you can pass a $raw variable to the data
| array. If $raw is true, then the template will be returned without
| rendering in raw format. This is helpful if you want to use a Handlebars
| template clientside with javascript.
|
*/
'optional_raw_output' => true,
/*
|--------------------------------------------------------------------------
| Translate Raw Output
|--------------------------------------------------------------------------
|
| If language_helpers and optional_raw_output are set to true, this option
| can also set to true. If so, the translation helpers will also be
| rendered for the raw output.
|
*/
'translate_raw_output' => true,
];
I have tried to create a helper function to render dynamic partials in my code.
I've added these in the config/handlebars.php
file:
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_RUNTIMEPARTIAL,
'partials' => [],
'partialresolver' => function ($cx, $name) {
if (file_exists(resource_path("views/partials/$name.hbs"))) {
return file_get_contents(resource_path("views/partials/$name.hbs"));
}
return '';
},
'helpers' => [
'concat' => function ($path) {
return $path;
},
],
'helperresolver' => function ($cx, $name) {
if ($name === 'concat') {
return (function () {
return '';
});
}
return '';
},
I've added these in the resources/views/home.hbs
file:
<main id="main">
{{#each data }}
{{> (concat this.name) this}}
{{/each}}
</main>
I've made this helper function work, but the partial resolver is not working after the helper function is called.
For now we can pre-compile language variables on serverside (when option $translate_raw_output
is true
) and then output the raw template with compiled language variables. This is helpful, because language variables are unlikely to change and otherwise we would have to pass all language variables to clientside and compile them on clientside each time we use the template.
The same is true for some other variables. For example we have a post comment form, which shows the current logged in user. The current logged in user will be the same over the whole page. So it would be nice to compile all user information on clientside and deliver the raw template with compiled user information to the client.
Solution:
{{const var}}
or {{fix var}}
, where var
can be any variable that should be compiled on serverside even for raw output (e. g. {{const username}}
).$translate_raw_output
to $prepare_raw_output
, so this option refers to language AND constant variable compilation.Hi there, I was trying to setup your repo when I got this error on running composer update.
zordius/lightncandy dev-master -> no matching package found
I believe altering the composer.json include line to
"zordius/lightncandy": "^0.20.0"
will fix this
Hi there,
I seem to be getting a lot of caching problems with my content. It is easy to fix but gets quite mundane to delete multiple files everytime you make a change to a template.
May I suggest implementing something where the modified dates of both the template and the cached file are compared and if the modified date of the template is newer than the cached file then the cache gets regenerated.
I have done this in laravel 5 before and would be willing to contribute code if needed.
They are going to release a new version soon. The new version fixes several things, an important one is if the context changes on ifs inside block helpers zordius/lightncandy#182
I just add it to keep in mind.
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
Hi again, sorry just pointing out another small thing.
When I was trying to set this up and publish the config I originally got a view not found error.
Looking at the config I realize you have this line
'fileext' => [
'.handlebars',
'.hb',
],
Adding .hbs to this works but is the "hb" a typo or is that another file extension people use for handlebars? I know hbs and handlebars are used quite commonly as the extension.
You need to publish the config using
php artisan vendor:publish --tag=config
I do not see that in the readme, but its pretty important as you discuss configuration...
Esp since you do not get support for .mustache file without modifying that file... it would also be nice to support those out-of-the-gate.
This line uses the following syntax for passing a function to array_map
:
$data = array_map('self::convertObjectToArray', $data);
PHP 8.2 has deprecated this callable format, because it does not always work properly.
The following syntax should provide equivalent behavior, and is not deprecated:
$data = array_map([$this, 'convertObjectToArray'], $data);
(There are several other equivalent syntaxes, but this is the one I personally like best.)
This line creates an $options
property on the HandlebarsCompiler
object, which was not previously defined.
The class should probably have $options
defined as an explicit property.
hi there,
When updated lightncandy to v0.89, it only looks for partials in resources/views
I'm using pingpong modules
downgrading solved the issue, but latest laravel handlebars, requires lightncandy v0.89
Thanks for this package and I realise it's quite old but it's an important piece of my app.
Is Laravel 8 compatibility just a matter of updating the dependancies? I'm more of a front end guy and have explored trying to test this locally but have not had much luck.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.