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
Related
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'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.
Here i was i have :
A RESTful Symfony API that i created with few bundles
A front AngularJS that i have in the web repository
Now here is a sample of my routing file :
$routeProvider.
when('/liste-produits', {
templateUrl: '../templates/list-products.html',
controller: 'ProductListCtrl'
}).
otherwise({
redirectTo: '/'
});
The fact that i have to use "../". Because otherwise it won't work in dev environnement (app_dev.php). And of course by the time i will post it in production (app.php) i won't need to add this "../"
Do you guys understand my problem ?
Since i can get assetic from Symfony work in the routing file.
How can i solve this ?
There is an approach, where you define a global variable in your base twig file:
Symfony 2:image paths in javascript file with assetic which you can in turn use in e.g. AngularJS.
There is also a bundle called FOSJsRoutingBundle, it sort of exposes your routes to the client and thus javascript. That might be interesting for you.
However there is another option; - I have personally used the approach posted by Rein Baarsma with the twig file and then cached the resulting javascript.
It's fairly simple to write a request listener that renders the twig file to a javascript file once a day or whenever the javascript file is deleted.
I used the same approach with the stylesheets for a project with daily changing colors.
If you do not cache it, the browser will revisit the route returning the javascript on each page and rerender the javascript file, which adds a lot of overhead.
You could simply make a Symfony Controller with a view on a js file. That way you can use the twig (or php) template functions (like twig's path() function) to avoid any strange urls.
In your controller:
public function routingAction(Request $request) {
$this->render('angular/routing.twig.js');
}
And in your routing
$routeProvider.
when('/liste-produits', {
templateUrl: {{ path('product_list') }},
controller: 'ProductListCtrl'
}).
otherwise({
redirectTo: '/'
});
I heve an embedded controller in my base template. It's a search bar.
For the search bar controller, I have a route "myProject/search".
What I would like is that this route will be taken only when the template where I am embedding the controller (base.html.twig) will call it, and not when i manually put in the browser: "myproject/search".
Any idea on how to do that.
I think, since some time you can't do it:
http://symfony.com/doc/current/book/templating.html#embedding-controllers
quote from the docs:
Even though this controller will only be used internally, you'll need
to create a route that points to the controller
(...)
Since Symfony 2.0.20/2.1.5, the Twig render tag now takes an absolute
url instead of a controller logical path. This fixes an important
security issue (CVE-2012-6431) reported on the official blog. If your
application uses an older version of Symfony or still uses the
previous render tag syntax, you should upgrade as soon as possible.
Anyway, I guess, you can try do it yourself by passing some "secret" argument to search action when you call it from your template. Next in the action you check if the argument was passed to it, and if not you throw 404.
Another way to achieve your goal is use .htaccess file.
You can restrict your route to a certain method by _method option in your routing configuration:
your_rote:
pattern: /myProject/search
defaults: { _controller: YourBundle:YourController:YourAction }
requirements:
_method: POST
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.