i want to use custom code in blade template engine such as custom function for use into that
my function:
public function viewCurrentDate( $arg=0 ){
return 'date is'.date('y');
}
in Blade template use like with this code
#< viewCurrentDate >#
how to develop blade for custom actions?
Add in the filters.php file
Blade::extend(function ($view) {
return str_replace("#dateY", 'date is'.date('y'), $view);
});
in template :
<h1>#dateY</h1>
Full tutorial about blade extension here : http://blog.zerilliworks.net/blog/2013/04/03/blade-extensions-in-laravel/
This is possible (see markcial's answer) but I think there is a much better way to do it.
Why not just set up a HTML helper class.
class HTMLHelper {
public static function viewCurrentDate($arg = 0){
return 'date is'.date('y');
}
}
You can then add this to echo the date in your blade file:
{{ HTMLHelper::viewCurrentDate() }}
It is slightly more characters (compared to what you wanted), but will be so much more flexible for you as you can add as many helper methods to your class as you like and use them anywhere, not just in blade.
Edit: Whilst markcial's answer is what you are after (tells you how to create a new template string in blade) I'd don't think that is the simplest way to do things. The helper file is much more flexible and reusable. For example, you can use this helper file ANYWHERE in your app. With markcial's answer, you are only allowing blade to use what you have written. To me, it doesn't seem worth it when a more flexible, easier solutions is available.
Related
I am working on the front end of a Laravel project and I can change all the values in the view templates. I can probably modify other files as well, but, as I don't yet grok Laravel Blade fully, and I have a time constraint, I'd prefer not to make life harder for myself.
What I want to do is output some data related to the current route, and retrieve and parse some data from the Resources/lang/values.php file. Can I do this within the view without inserting a bunch of messy php? Is this a stupid thing to do? Is their a best practice for this?
Thanks in advance.
Use __() or trans() helpers or #lang Blade directive to work with language files:
{{ __('values.some_string_from_values_language_files') }}
Or:
#lang('values.some_string_from_values_language_files')
These helpers will work only if values.php is in:
resources/lang/en/values.php
resources/lang/fr/values.php
....
An answer to your question about best practices is no, you shouldn't reinvent the wheel and keep language files in a standard directory.
To get current route data, use Route facade and these methods:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.5/routing#accessing-the-current-route
A started to work with Phalcon framework, included Blade templating. It's already works, but unfortunately i didn't find the right way to include css and JS assets in master.blade.php.
If I add the assets like $this->assets->addCss("css/bootstrap.min.css"); in the controller I can not include it in the master template file.
For example, my indexAction looks like this:
public function indexAction(){
$this->assets->addCss("css/bootstrap.min.css");
$this->assets->addJs("js/bootstrap.min.js");
return $this->blade->make('index.index');
}
Thanks for any help!
Well - you should add blade as actual template engine into phalcon view.
Your class should extends Engine implements EngineInterface. If you will do it it could be nice to add it to incubator repository.
https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/View/Engine check out implementation of other engines for more how are they made. Then you could just do {{ assets.outputJss() }}, example from volt/twig, not sure how exactly it should look like in blade, never used it.
Also what's wrong with volt? It's faster than blade and have many features.
I used to use smarty a lot and now moved on to Laravel but I'm missing something that was really useful. The modification in the template of you're variable.
Let say I have a variable assign as {{$var}}. Is there a way in Laravel to set it to upper case ? Something like: {{$var|upper}}
I sadly haven't found any documentation on it.
Only first character :
You could use UCFirst, a PHP function
{{ucfirst(trans('text.blabla'))}}
For the doc : http://php.net/manual/en/function.ucfirst.php
Whole word
Str::upper($value)
Also this page might have handy things : http://cheats.jesse-obrien.ca/
PHP Native functions can be used here
{{ strtoupper($currency) }}
{{ ucfirst($interval) }}
Tested OK
You can also create a custom Blade directive within the AppServiceProvider.php
Example:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
Don't forget to import Blade at the top of the AppServiceProvider
use Illuminate\Support\Facades\Blade;
Then use it like this within your blade template:
#lang_u('index.section_h2')
Source:
How to capitalize first letter in Laravel Blade
For further information:
https://laravel.com/docs/5.8/blade#extending-blade
works double quoted:
{{ucfirst(trans("$var"))}}
I'm writing a website using Slim Framework and Twig.
The sidebar on the website will have dynamically created links and i want to get the links from the database/cache every time a page is rendered, i can do this in each controller action but i would like to do this in my base template so i don't have to get and render the data manually in every controller action.
I have looked through the twig documentation and I haven't seen anything that could be of use besides the include function/tag but i don't see how i could get the data easily.
Is my only option to write an twig extension to do this or is there an easier way?
First of all: templates usually shouldn't contain any type of bussines logic but only render the data you provide it.
However you could write a custom twig function and use that to aquire the menu data from the DB in order to render it in your base template.
Alternativeley you could write some Slim middleware that aquires the data which might be able to inject the data into the template.
Hope that helps.
After reading Anticoms answer i was able to do it by writing a Twig extension.
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('render', array($this, 'render'))
);
}
public function render($template, $controller, $action) {
$app = Slim::getInstance();
return $app->$controller->$action($template);
}
Now i can simply write
{{ render('Shared/sidebar.twig', 'Controller', 'Index') }}
Into my twig template to render the template with the data i want.
Note that my render() function uses slim dependency injection to instanciate the controller at runtime.
There’s no setTemplate() for components! I know but maybe there is another way to do it ?
(The question seems to be about a php framework: http://www.symfony-project.org/)
There is no setTemplate method on sfComponents. You essentially have 3 options:
Name your component the same as the partial you'd like the component to render. This may not be possible if you have multiple components you'd like to share the same template.
Create a partial with the same name of your component and include the partial there. That is, if you had a component with an executeFoo() method that you wanted to render the _bar.php template, simply call include_partial('bar', $vars) inside of _foo.php.
Load the PartialHelper and render the partial manually inside of the components execute method and have the component return sfView::NONE.
Components don't handle templates, you can only use partials. If you need to return a specific partial from inside your components class you can do something like this:
return get_partial('module/action', array('paramName' => $paramValue));
Have a look into the symfony book, chapter 7 view layer
To get around this, i'm doing:
echo get_component('module', 'action', $this->getVarHolder()->getAll());
return sfView::NONE;
This worked for me:
$this->setVar('template', 'templateName');
Obviously the template have to be in the exactly same module.