How to add helper class function in twig template with laravel - 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

Related

How to eval Blade variable when pulling variable from database

I am using Laravel 5.7 I am having a variable stored in the database and would like to have it evaluated by Blade before rendering.
Database
Field: name, Value: {{ $organisername }}
I am pulling out this piece of data into a Blade template like this.
<h4>1. Registration</h4>
<br />
{{ $job->organisername }}
Chrome then displays {{ $organisername }} instead of evaluating {{ $organisername }}.
1. Registration
{{ $organisername }}
If I type {{ $organisername }} into the Blade template, it gets evaluated to John when the page is rendered..
1. Registration
John
I'm using TinyMCE to input {{ $organisername }} into the textarea, and it gets saved into a Database, if that helps.
If by evaluate you mean to do some consistency for your variable, know that this Blade echo statement "{{ }}" already does that with htmlspecialchars, like explained in the docs:
https://laravel.com/docs/5.7/blade#displaying-data
Blade templates are compiled down into PHP before they evaluate. Essentially, blade compiles:
{{ $job->organisername }}
Into
<?php echo e($job->organisername); ?>
Once you understand this and how PHP evaluates variables, you should be able to understand why you can't set Blade constructs in the database and have them evaluated when it's printed.
It's basically the same as echoing a literal string:
<?php echo '{{ $organisiername }}'; ?>
You wouldn't want Blade or PHP evaluating any expression they come across as they print, that would potentially be very insecure.

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;
}

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

Blade templating engine - custom tags

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>

How to the domain url in laravel5

I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.
You can also try
{{ Request::server ("SERVER_NAME") }}
{{ Request::server ("SERVER_NAME") }}
Or go with
{{ Request::root() }}

Resources