Can I include PHP templates in a TWIG base template or vice versa? - symfony

I am currently in the process of refactoring and restructuring an existing Symfony 2 application.
There are a few base templates and all other templates inherit from them. Right now, all the templates are PHP. I am planning to migrate the application to TWIG.
The problem is that this application is already in use and while migrating I am also expected to add new features to the application. This means I can not freeze the current state of the application, fully migrate to TWIG and then continue working with that.
This means that a migration to TWIG is only possible in small steps, i.e. I can only migrate a handful of templates before I have to deploy a new release. There will also be some new templates for the features I am adding and I would also prefer to add these in TWIG format.
My plan right now is to migrate the base templates to TWIG first and then include the existing PHP templates in those new base templates. Afterwards, I would continue migrating the other templates in small batches.
Looking at documentation online, it is possible to have both the PHP and TWIG template engines enabled simultaneously. However, I could not find any tutorials, examples or documentation mentioning that you could not only have those two engines side by side but also have templates from one engine include templates from the other engine.
Is there a way to mix templates from those two engines as I described?

Even if there are similarities between both engines (they still do the very same thing), they are working internally in their own ways, the architecture and the code are very different.
Templates provided in PHP are managed by the Templating Component.
Templates provided in Twig are managed by the Twig engine.
Have a look to both codes, and you'll understand why we just can't compare/mix them.
For example:
slots from php engine plays with string overwriting
blocks from twig engine plays with class overloading
So even if you were able to access the twig-compiled file using a tricky import, you'll not be able to overload a block by the simple fact that your php template isn't a class.

Related

How to support multilingual for angular Website

I am beginner to Angular. i do not know how do support a website for multilingual using angular 11.And i have some dynamic contents in the website .I want this to include the multilingual support. can anyone please help me
There are different solutions. You can use angular built in i18n (https://angular.io/guide/i18n-overview). But it requires handling bundles with server because angular i18n creates separate bundle for each language you want in your application. For example, if user switches to english you should load bundle for english language and reload the page and same for other languages.
Another one is Transloco (https://ngneat.github.io/transloco/) which is popular. It will allow you change i18n in runtime without creating multiple bundles. You need to configure app to work with this package and create json files with keys and translated values for each language. Transloco has good documentation and examples.
Another popular is ngx-translate (https://github.com/ngx-translate/core) which works similar to Transloco.
Also if your project is small you can handle translations manually (update/add new ones) but in case of large project better way is to upload i18n resource files to 3rd party services where translator will translate everything and you can then import translated files to your project.
So it depends of your project which one to select but you can compare advantages and disadvantages and choose necessary.

Smarty vs Twig differences

My team is starting to develop some new middle-size website with CMS, we have our custom developed CMS but we are forced to use Symfony2 in this particular project, and because we have a lot of different code that we could reuse in a new project.
So I would like to ask: What is the difference between Smarty (we are using it in our CMS) and Twig (used in Symfony2)? Would you suggest to rewrite functionality from Smarty into Twig, or just add some Smarty libraries (bundles) into Symfony (if there are any of them available)?
Is there big difference between them? After quick read of Twig documentation it seems that there is no really big difference in syntax between them.
I don't know how much Smarty files you have, but if you have some files I recommend to rewrite them to Twig, as that is the default template engine which is optimalised in Symfony2.
If you have a lot of files, I recommend to add Smarty as a template engine in Symfony2 (with the Noiselabs/SmartyBundle) and use Smarty for your templates.

Where do I store services in Symfony2

I'm building an app in Symfony2 that receives a HTML string via an API, does some work on it (think of something like the Tidy library, only with a different purpose) and then returns the fixed HTML.
The stuff that does all the work is mostly written in services, because they don't need to return a Response object.
Where do I store those services? I'm putting them in bundles, but there's no obvious place to store them. I understand that they can be stored anywhere, but I'm wondering what the default convention is.
If that code doesn't have to be re-usable outside of Symfony2 projects, you can just put it into your bundle.
I'd go for something like:
Acme\HtmlCleanerBundle\Cleaner\SimpleCleaner.php
Since it is a bundle, you can extract it to a separate package and re-use it for other Symfony2 projects.
If you want the code to be completely re-usable and standalone, I'd suggest you extract it to a separate library. You can still have dependencies on individual components (shameless plug: you could manage those dependencies through composer, which will be used by Symfony 2.1).
If you decide to go down that path, the bundle simply integrates the library into Symfony2 by providing DI container configuration for it.

Symfony2 Backend?

I am new to Symfony, please don't scold me about this question.
I read in a Symfony 1.4 book that there is a functionality of adding backends to your applications via doctrine:generate-admin or generate:app backend, implementing CRUD operations.
Now I wonder, is there anything similiar with Symfony2.0?
Basically I have an user management and a database in the background that I would love to have available for my Admin Users, but do I have to code it myself (i.e. securing the are /admin, creating templates, classes, methods etc. that fulfill this tasks) or can it be done more easy by using some kind of an already existing backend functionality?
Thanks!
EDIT: Also read there's a plugin called "Apostophe" - but isn't there anything included with SF2 or are there any preconfigured SF2 Versions that already come with Backend solutions?
There's the SonataAdminBundle (and possibly a few more I haven't seen) that helps provide an admin system, however this area is still being worked on heavily by the Symfony2 community.
Lukas has put together a blog article on the current state of development of a Symfony2 CMF, with some rough timescales - State of the Symfony2 CMF
Hope this helps!
Also there is another bundle for managing the admin. You can take a look here http://knpbundles.com/cedriclombardot/AdmingeneratorGeneratorBundle
For "admin generation", Symfony2 is not yet with the code generation on that aspect. The base is VERY STRONG. Remember Symfony2 is still at 2.0 stage. (Not 2.1++ like symfony1).
If you want cut and paste code. Symfony2 is not exactly for that.
DISCLAIMER
I am more specialized in frontend than backend, but I got around stuff. But Thanks to Symfony2. I can be on both sides. So I speak from this context.
CAVEATS
What I call "good stuff".
Only using ONE {{ form_widget(form) }} that Symfony form documentation recommends ... and not explictly cut and pasting from the Twig tutorial that is /meant/ to show how to do theme... not to implement like it is shown.
Thinnest controller methods possible
... out of scope of this post.
SO
My best advice is serious RTFM, a few times, with Symfony books. It is scary, but I am working with it since three months and re-reading a lot. Everytime I get excited with gems.
ANSWER IN QUESTION
My current choice of bundles is:
FR3DLdapBundle, with FOSUserBundle
MopaBootstrapBundle along with all code generated with HTML/CSS structure Twitter Bootstrap gives. (if you haven't heard of it, yet, where were you?)
AsseticBundle with proper {%- javascript %} inclusion
And the rest is contextual.
My READ and RE-Read list
A hidden gem and very helpful blog posts were for me from:
Problematic's blog post series "On a diet"
Matthias Noback
Liip Symfony2 tagged blog posts, but mostly the Table Inheritance topic
and CraftItOnline
For the sake of completeness there is a native way to generate CRUDs:
php app/console doctrine:generate:crud
This command will ask to you some information, like the entity, if you want a write action, the configuration format or the base route prefix.
In my opinion this is the fastest and light way to quickly generate a controller to create, read, update or delete your entities, so it is good on simple projects or to code a basic CRUD inside a little Bundle.
Official documentation: http://symfony.com/doc/master/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html

An exact description of a Symfony Bundle in a complex web application

I'm new to version 2 of the Symfony framework. I made some projects with v1 but now trying to get my head around the new version and it's features.
I read over the concept of Bundles but it's purpose is not yet very clear to me.
Say you have a big web application, a CRM for example. How would the bundles look like?
Would it be NewsletterBundle (for sending newsletters), ContactManagementBundle (for managing contacts), UserBundle (for editing users and their permissions).
Or would it be less cut-up like, EmailBundle (for handling the entire email traffic), CRMBundle (for putting in all your CRM code), PermissionsBundle, ApiBundle.
I like to think of it like this: a bundle should represent a specific feature or set of like features for a project.
Your first example is a better use of bundles than your second example, because the purpose of each bundle is more defined. While it's possible to use one CRMBundle for everything, you wouldn't really be taking advantage of Symfony's ability to organize your code. Additionally, if you wanted to port over your Newsletter code to a new project, but not all of the CRM code, you'd have an easier time copying over a NewsletterBundle versus copying over the CRMBundle, and then pruning it.
When thinking about a Symfony2 project, sometimes you want to forget everything you know about symfony 1.x, since they take wildly different approaches to solving many problems. For example, in symfony 1 it was common to build a 'frontend' and 'backend' app for a project, and each app would obviously contain logic specific to those parts of the project. So you might have a Newsletter controller in both the frontend and backend apps. In Symfony2, you're better off using only one Newsletter bundle, but with two controllers (perhaps named 'frontend' and 'backend'). Again, an immediate benefit to this is how reusable your code becomes.

Resources