I am trying to work on a project that uses Twig Template. Right now, I am learning it and have checked if I can do any alternate for the project as I know how the MVC architecture works. The Twig Template is different as it has file extension html.twig and the following in it:
{% for user in users %}
<li>{{ user.username | e }}</li>
{% endfor %}
Now for alternate purpose, I've created a controller and view (Not using Twig rules) in the Template as follows that's very basic:
Controller:
public function index()
{
//return something
}
View:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>This is a sample page.</p>
</body>
</html>
As you can see, I am not using the default architecture for the project (It works) and thinking not to use Twig architecture for the time being. So my question is if I can continue with raw PHP and basic HTML in the Twig Template architecture? Any idea would be appreciated.
If the view files are being run through Twig, you won't be able to put any PHP statements inside them.
But you can certainly put plain HTML in Twig files, as you've found.
You can use php templating with our without twig
framework:
# ...
templating:
engines: ['twig', 'php']
Render controller in php:
<?php echo $view['actions']->render(
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
'App\Controller\HelloController::fancy',
array(
'name' => $name,
'color' => 'green',
)
)
) ?>
Related
I'm a beginner in Symfony and I am trying to set up CKEditor for the Sonata News Bundle.
I have looked everywhere (even on German forums whereas I don't speak German !) but I cannot find any answer anywhere.
Does anyone have a clue or a solution to my question ?
Thank you everyone.
Louis
I would suggest you to use IvoryCKEditorBundle
Download the Bundle:
composer require egeloen/ckeditor-bundle
Register the Bundle, Then, update your app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Ivory\CKEditorBundle\IvoryCKEditorBundle(),
// ...
);
// ...
}
}
If you're using Symfony <= 2.8:
php app/console ckeditor:install
php app/console assets:install web
If you're using Symfony >= 3.0:
php bin/console ckeditor:install
php bin/console assets:install web
Then usage
The way I do it (symfony: 4.3.5, sonata: 3.54.1).
Download ckeditor JS lib and place it inside public/js so i.e. you should have the file:
your_app/public/js/ckeditor/ckeditor.js
and beside that file dirs: adapters, lang, plugins and skins and some js/css/md files.
Then override base sonata edit template with:
{% extends '#SonataAdmin/CRUD/base_edit.html.twig' %}
{% block javascripts %}
<script src="{{ asset('js/ckeditor/ckeditor.js') }}" type="text/javascript"></script>
{{ parent() }}
{% endblock %}
This code will include ckeditor to your pages and should be placed at: your_app/templates/edit.html.twig
Then, add to "templates" section edit:edit.html.twig so template you created would be used:
sonata_admin:
title: 'Your app Admin'
dashboard:
blocks:
- { type: sonata.admin.block.admin_list, position: left }
templates:
edit: edit.html.twig
This is file: your_app/config/packages/sonata_admin.yaml
Then, when you are adding new field from sonata admin bundle, inside configureFormFields your ckeditor fields should look like:
->add('field_name', null, array('attr'=> array('class' => 'ckeditor')))
Important part is adding array of attributes with class attribute.
Clear the cache and CKEditor should be working now.
I'm starting a project using Phalcon framework with Volt as a template engine. I have some experience with Symfony/Twig.
I read documentation and tried searching all over the internet but can't find a satisfying way to accomplish what I want (I find ugly the solution described here: How do I create common template with header and footer for phalcon projects with Volt Engine; it's not using Volt per se for the navigation.)
So the story is pretty easy: my base template consists of 4 parts: navigation, header, content and footer. I use partials to include the common areas in the base template like the navigation, header and the footer, works fine with "static data".
Now the question is: how do I get to dynamically generate the navigation menu with items coming from the database? The template will have common areas that have to come from DB also in the header, footer and a sidebar. Having to fetch that in all Controller actions sounds like overkill and not very DRY (maybe do it on the init part? but will have to be done in every controller. Maybe in an abstract controller, I dunno.)
What is the best way to accomplish this in Phalcon/Volt? In Symfony/Twig you can call from the view a controller action, so you can have like a LayoutController that renders partials from a page.
Thanks!
Here are few variants:
1) Your controllers can extend a BaseController and in it's initialize() method you can assign those variables to the view.
class BaseController extends \Phalcon\Mvc\Controller
{
public function initialize()
{
// Common Variables
$this->view->assetsSuffix = $this->config->debug ? '' : '.min';
}
2) Create a custom Volt function which loads the data.
// In your Volt service:
$compiler->addFunction('getMenu', function($resolvedArgs, $exprArgs){
return 'Helpers\CommonFunctions::getMenu(' . $resolvedArgs . ')';
})
// Helper file and function
public static function getMenu()
{
return \Models\Menu::find();
}
// Volt usage
{% set menuItems = getMenu() %}
{% for item in menuItems %}
{% endfor %}
3) Use the models to query the DB directly from the template. However this is not yet supported with Volt (not sure if it is added in latest version, have to confirm).
<?php $menuItems = \Models\Menu::find(); ?>
{% for item in menuItems %}
{% endfor %}
4) Ajax/Javascript, but this is really dependant on your application. Something like Angular approach, but I will not get into details with this varaiant.
Hello so I have a simple code here that will render home.html using slim framework and twig. Here's the codes:
In my index.php file:
require_once 'vendor/autoload.php';
$app = new \Slim\Slim([
'debug' => true,
'templates.path' => 'app/views'
]);
$app->view = new \Slim\Views\Twig();
$app->view->setTemplatesDirectory("app/views");
$view = $app->view();
$view->parserOptions = ['debug' => true];
$view->parserExtensions = [new \Slim\Views\TwigExtension()];
$app->get('/home', function () use ($app) {
$app->render('home.html');
});
$app->run();
Here is the base.html template:
And my home.html:
{% extends "base.html" %}
{% block content %}
Some content here
{% endblock %}
My question is, since the only rendered part is the home.html, what if I want some data loaded in my base template? Like this..
So that I won't have to repeat it on every page I render. Is that possible on a base template? Thank you in advance.
Also, this is what I followed to install twig in slim.
I think the best answer is given here. There the answerer say "write a Twig custom function" to load dynamically data in your views.
So you would be able to write your own PHP, can inject your DB and use this along with that templating engine.
How to register js,css file to head tag from inside bundle automatically? so we don't need to add it manually to layout.
In zend framework there is HeadScript and InlineScript Helper, prepend or append method to do this. How about symfony2 ? is it possible too?
In zend framework we can register it like this from module bootstrap :
public function onBootstrap($e) {
$sm = $e->getApplication()->getServiceManager();
$headLink = $sm->get('viewhelpermanager')->get('headLink');
$headLink->appendStylesheet('/assets/MyModule/css/mystylesheet.css');
}
I mean, I want register it from e.g AcmeBlogBundle.php so we don't need to add manually to <head></head> in layout.
I'm not sure if this is what you are looking for, but check this out, this shows you how to include every js-file inside a directory, without listing them all manually.
You can't do it like in Zend. But you can define the appropriate block with js in base layout only once. And then from any template you can put anything you want into this block. Symfony implements a bit different logic than Zend does.
In your layout:
<head>
{% block custom_head_js %}
<some basic js files for all the pages>
{% endblock%}
</head>
In your templeate:
{% block custom_head_js
'#YourBundleNamespace/YourBundleName/Resources/public/js/your_js_filename.js'
%}
<script src="{{ asset_url }}"></script>
{% endblock %}
So, as I said it's not necessary to change base layout every time you want to add js into 'head' js files - you have to do it only once.
I used to work with CodeIgniter. Now I am starting to learn Symfony2. I was just wondering, in CodeIgniter I could load a view from another view. Like, I could load menu.php from index.php. This is how I used to do it -
//in index.php
<?php $this->load->view('menu.php'); ?>
Is it possible to do the same thing in Symfony2 and Twig?
There are a few different ways you can do it depending on what you're trying to accomplish.
If you want to render the response of a controller you can do this in your twig template.
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
In the above example, the parameter passed max would be passed as an argument to your controller. Then the controller would be responsible for returning a response that will be inserted into the view where it was called from.
You can also use include to render just the twig template as an embedded view:
{% for article in articles %}
{{ include(
'AcmeArticleBundle:Article:articleDetails.html.twig',
{ 'article': article }
) }}
{% endfor %}
In the above example article would be passed into the context of the twig template articleDetails.html.twig but not to any controller. So this method is ideal for repetitious front-end code that is used in many places such as templates for tables, lists, sidebars, etc.
http://symfony.com/doc/current/book/templating.html#including-other-templates
http://twig.sensiolabs.org/doc/functions/include.html