How to add assets in Phalcon framework with Blade template engine? - php

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.

Related

How to link twig template in href?

How can I link a twig template? Something like this:
Learn more
Is there a way to do this or do I have to use URL rewriting?
You can use template controller for this. Just add new route, specify template controller FrameworkBundle:Template:template and twig template.
routes.yml
qwerty:
path: /qwerty
defaults:
_controller: FrameworkBundle:Template:template
template: Events.twig
and change this link to:
Learn more
http://symfony.com/doc/current/templating/render_without_controller.html
Your question is conceptually flawed. Twig is a templating language, and cannot be rendered as a web page on its own.
You probably don't want to link directly to a .twig file (in fact, .twig files should not be directly be accessible in your public document root at all)! Rather, you need some PHP code (either a standalone script, or preferably, a front controller route) that renders your template in the context of your application, and returns the fully rendered page at a URL that you have specified.
For example, in the Slim framework, you might do something like:
// Render Twig template in route
$app->get('/events', function ($request, $response, $args) {
return $this->view->render($response, 'events.html.twig');
});
// Run app
$app->run();
See this article for more information on moving away from procedural spaghetti code and thinking in MVC terms.
If you are using twig the template will need to be parsed, so you cannot link in the way you have tried. In Symfony 2 you would use something like {{ path('name_of_path') }}, you will need to name your route first though.
You can name the route with name="event" with Annotations:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* Matches /event exactly
*
* #Route("/event", name="event")
*/
public function eventAction()
{
// ...
}
In the twig template link with this:
Event
More info here:
http://symfony.com/doc/current/templating.html#linking-to-pages
You can't. Make another controller action that renders Events.twig and link to the respective route using path():
Learn more
https://symfony.com/doc/current/controller.html#a-simple-controller
http://symfony.com/doc/current/routing.html

Add custom class to main blade template

My app.blade.php file is my main file that will #yield all my other blade template files. I noticed that the Auth class is available to this file. I cant figure out how to add one of my custom classes, Projects so it is available to app.blade.php.
app.blade.php contains a drop down menu where I need to list all my projects, but if I try something like
Projects::where('user_id','=',Auth::user()->id)->get()
I get an error saying the Projects class is not found. How can I add this to my master template just like how Auth is available? Or, do I need to go about this differently?
You do it by injecting the class into your template.
#inject('project', 'App\Project')
{{ $project->where('user_id','=',Auth::user()->id)->get() }}
Just make sure your namespace is correct.
http://laravel.com/docs/5.1/blade#service-injection

Laravel develop Blade template Engine

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.

How To Include PHP Files In Twig Files In Symfony2

I am trying to insert a latest tweets "widget" on to a Symfony2 project. I have found an ideal script written in PHP that will do the job perfectly.
However, I don't know where the best place to put 3rd party PHP files in a Symfony2 project. I have placed them in the same folder as all my twig files reside, changed the name to read tweets.php.twig, and even located them in the web folder. When I try to include the file in the twig file that needs the Twitter feed it comes up with an error saying that it can't find the file.
Do I have the right idea, or do I have to convert the PHP in to a twig file or write the PHP script in to a controller?
I believe the recommended way would be to create a Symfony2 bundle that encapsulates all the logic for the tweet widget. You would then call your bundle controller and pass the response to your twig template.
If that is too complicated or you want something more quick and dirty - you can create a controller like TweetWidgetController.php and put the code into there as an action like widgetAction. Just make sure you return the tweet widget output in a Symfony response object.
Then from your main controller - you can do something like
$widget = $this->forward('YourBundle:TweetWidget:widget', array('twitterid' => 'yourtwitterid'));
return $this->render('YourBundle:yourtemplate.html.twig',array('widget' => $widget->getContent()));
Now in your twig template you can put it wherever you'd like by referencing it as:
{{ widget }}

Symfony: Is it possible to setTemplate for components?

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.

Categories