Blade templating engine - custom tags - laravel

I've seen some mentions of custom tags for the blade templating engine, but can't find an extensive documentation for blade.
Is there anything similar to {{ book.name|capfirst }} from the django templating engine in blade?
Is twig a nice alternative that could suit my needs?

If you want to make first character uppercase, use
{{ ucfirst($variable) }}

You can create a custom blade directive. Register it in the App\Providers\AppServiceProvider's boot method:
public function boot()
{
Blade::directive('ucfirst', function($string) {
return "<?php echo ucfirst($string); ?>";
});
}
Then in your blade views you can do:
<h1> #ucfirst($myString) </h1>

Related

What's the equivalent function of i18n t() function in Laravel?

In i18next, people are defining t() function as translation utility, vue has vue-i18n, react i18n also has such thing.
Here comes my question, what's the equivalent t() function in Laravel?
In Laravel you can use __ helper function for the same.
As per Laravel documentation:
You may retrieve lines from language files using the __ helper function. The __ method accepts the file and key of the translation string as its first argument. For example, let's retrieve the welcome translation string from the resources/lang/messages.php language file:
echo __('messages.welcome');
echo __('I love programming.');
If you are using the Blade templating engine, you may use the {{ }} syntax to echo the translation string or use the #lang directive:
{{ __('messages.welcome') }}
#lang('messages.welcome')
Reference:
Laravel -> Localization -> Retrieving Translation Strings
trans('messages.welcome'), trans_choice('messages.apples', 10), and #lang('messages.welcome')
TO display in the blade wrap those in {{ }} like {{ trans('messages.welcome') }}
To display html contained in them in blade use {!! !!} like {!! trans('messages.welcome') !!}
https://laravel.com/docs/master/localization
https://laravel.com/docs/master/localization#pluralization
https://laravel.com/docs/master/localization#retrieving-translation-strings

Twig Naming Convention of Webforms

I am using Drupal 8. I have a web form named careers_application (machine name). I need to create a twig template for it. I tried to use webform-submission-form-careeers_application.html.twig, but it doesn't make any sense to web form at all.
Using hook_theme_suggestions_alter, you can create whatever template suggestions you like. For example:
function Yourtheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if ($hook == 'form' & !empty($variables['element']['#id'])) {
$suggestions[] = 'form__' . str_replace('-', '_', $variables['element']['#id']);
}
}
then you would create templates in your theme, such as
form--user-register-form.html.twig
or
form--webform-submission-form-careeers-application.html.twig
You can just use the suggestions, and it will do the rest, after you clear cache.
You can render your elements within the template.
For example:
<form{{ attributes }}>
{{ element }}
</form>
Attachments:
https://drupal.stackexchange.com/questions/249856/custom-registration-twig-template
https://drupal.stackexchange.com/questions/220415/how-to-create-twig-template-files-in-drupal-8-for-customizing-user-login-profile

October CMS and navigation on current page

I am trying to get the basically the active class applied to the current page. As it goes, the builder plugin is setting the URL through:
<a href="{{ detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }) }}">
However I am new to October so I am not sure how to reference this.page.id in comparison to the url set above.
Basically I want this:
{ set UrlParam = detailsPage|page({ (detailsUrlParameter): attribute(record, detailsKeyColumn) }
{% if this.page.id == UrlParam %} class="active" {% endif %}
Any ideas?
One of the best debugging plugins out there for OctoberCMS is this: https://octobercms.com/plugin/davask-dump
That plugin makes connecting your twig templates to your database / php rendering a breeze.
After installing that plugin you can use {{ d(variable) }} instead of {{ dd(variable) }} and get more information on the array nests etc.
So I would do {{ d(UrlParam) }} and {{ d(this.page.id) }} in your twig template. See what the dump has to say about each of those variables. For clarity I do believe you need the % here {**%** set 'variable' **%**}.
I am also not a fan of the builder component and I use the PHP section on the page / partial pages. And establishing a class with the use function and access the data with $this['variable']. Maybe worth looking into here is a quick example:
Pluginauthor\Plugin\Models\Plugin;
function onStart() {
$plugin = Pluggin::all();
$this['plugin'] = $plugin;
}

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

How to add helper class function in twig template with laravel

in blade template I have used like:
{{ Helper::getName() }}
But how it is used with twig template?
If you are using laravel-twigbridge you can use helpers from default Extensions, e.g:
{{ form_open(...) }}
instead of
{{ Form::open(...) }}
See docs

Resources