Is there a way to use Volt templates in Symfony2? - symfony

I'm developing a system where a Phalcon server is responsible for a site frontend while a Symfony2 server is responsible for the content management. The content manager contains an WYSIWYG editor and thus displays the content using the same layout and style as the frontend. I realize Volt and Twig and very similar, but some things like includes and blocks have specific syntax or limitations. We can create our templates/themes in a way that is completely compatible, but that would not be very flexible.
Is there a way to use Volt templates in Symfony2? Or a practical way to convert templates automatically (by hand is not an option here)?

I think in your specific use case the most practical would be to use Twig with Phalcon. In your DI you can load a different engine for that, see the Phalcon Incubator project for the Twig-adapter.
In short it comes down to using (after adding the Incubator to your project, for instance with Composer):
$view->registerEngines(
array(".twig" => 'Phalcon\Mvc\View\Engine\Twig')
);
instead of
$view->registerEngines(array(
".volt" => 'Phalcon\Mvc\View\Engine\Volt'
));
The link describes a more complete step-by-step guide.

Related

How to organize templates in Symfony

I have an application build with Symfony 3.3 and Twig that will be distributed to more customers. I'm using parameters.yml to customize its behavior and it seems to work well.
Where I have an issue is with twig templates: although the customers will use most of the templates as they are, they'll need to customize some parts like CSS styles, general layout and do occasional design overrides.
The options I have identified are:
Have a full set of templates for each customer. The issue is that the upgrades will be a nightmare as we have to patch each template and account for differences
Customize templates via YML files. The problem is that it gets too complicated soon and the number of parameters is potentially huge
Deliver a set of templates in app/Resources/views and allow the customer to override any of the templates by creating another file with the same name in another folder
Deliver a set of templates in AppBundle/Resources/views and let customers override them in app/Resources/views
Create the application as a Symfony bundle (ie. MyAppBundle) and deploy the application to each customer by including the MyAppBundle via composer. I like this solution a lot, but I do not know whether it is possible to implement is easily.
Do you have any suggestion on how to approach this problem?
I think the best way in your case is to think about the main parts and the customer parts and then build blocks and overwrite all parts that are relevant for the customer for example the CSS or anything else. If you know that there are special part you can build macros for example.
With the Block concept you can predefine the template but the customer has the ability to overwrite that part you've defined with it's own.
With Twig you have a lot of possibilities to solve such a problem.
Drupal 8 use that Theming concept.
https://www.chapterthree.com/blog/twig-concepts-drupal-8-themes-part-ii
Perhaps you can take a look how they have solved that and you can find a good solution for your problem.

MeteorJs show custom version of same app to certain users

I'm working on re-developing an existing project written in php. The current company offers its users a 'bespoke solution' - meaning if they want certain parts of the software tailored to their needs.
From speaking to the developer, users are then moved from the 'global' version to their own 'custom' version and any updates that are rolled out to the 'global' version are manually then added to each custom version later.
In meteor, how would one go about replicating this? I'm thinking storing an array in the database with "their custom template names" and then with each page load, check database if any custom templates exist for that user - if yes then load that template else load the default (global) template.
The alternative is to clone the default project for every customer and spin up a separate server for them (Very expensive!!)
How does Facebook etc gradually roll out features - i believe its a similar concept.
Or is there a better solution to this?
If customization is only done at the level of templates then your proposed approach would work using Meteor's dynamic templates capability.
{{> Template.dynamic template=myTemplateName }}
You would define a helper for the enclosing template that would return myTemplateName. That function could execute your logic of looking for a custom template and if not found, defaulting back to a standard one.

Modular architecture to encapsulate functions in Symfony2

What I want to develop....
I am currently developing a SaaS-solution (Symfony2) to build HTML-presentations or better slide shows. Users can log into an administration UI and create presentations. This presentations can be played later. A presentation consists of elements. The elements can be images, texts, videos, pdf and more.
This element types should be encapsulated from the user interface. So extern developers can develop such new modules (element types) with defined interfaces and inject it into the system.
If a new module is finished, we move it to a specific directory in the symfony2 directory and the system detect the new module. No hardcoded-changes in the administration ui system should be neccessary.
Each module has a own "product-number". So we can use a database to enable or disable modules for different users.
What functions must be implemented by a module?
In the administration ui the user has the possibility to create a presentation and add elements of different types (the modules). User sees a timeline with the elements. When he clicks on an element, the editor for this element will be shown under the timeline (Parent-Child-View). Each module has different editors. A text-module needs other configuration-possbilities than an image-module.
So, in admin UI we need functions to edit elements:
createNewElement() creates new entry in the database (module has own tables, e.g. mod_text_elements)
renderEditorView() generates HTML which will be shown in the administration ui with module-specific inputs. E.g. image-upload/crop, WYSIWYG-editor, ...
saveEditorInputs() the module needs to handle the data entered in the editor and save it to the elements entry in the database
deleteElement() to delete an element
The output for the presentation:
Later we want to play this presentation. So a loop goes through the presentation elements and asks the modules of the element what and how to show it.
renderPresentation() to render (HTML) an element for the presentation
Database
I've made a little er-diagram of a possible solution to save the information about modules and elements in a database. The yellow entities are module-specific tables.
How to realize this in Symfony2?
My frist idea is to define an interface which have to be implemented by the modules. But which class has to implement this? Controllers?
Are modules = bundles?
How to realize the View-in-View of the editor? The editor of the module (e.g. renderEditorView()) should be viewed in the user interface of the administration ui.
I'd do it by hooking in to the EventDispatcher Component.
Your 'Master' Controller would define a set of events which correspond to the standard CRUD actions you've defined above ... onCreateNewElement(), onRenderEditorView(), etc. You'll probably find a dozen more to provide hooks for as you build your application (allowing plugins to add tools to a toolbar for example).
You'd define a Service (this doesn't HAVE to be a Controller Class), which looks for new 'modules' and adds their correctly-named methods as listeners for your custom application events.
Are modules = bundles? Well, that's entirely up to you. Are you going to provide a method for users to 'install' new modules specifically for your application? Then what a module needs to look like is entirely up to you. Are you going to slip Composer into the mix and allow modules to be installed that way? Places a couple of constraints on the structure, but still pretty much up to you.
How to realise the View-in-View? Again, that comes down to exactly how you define the interface for your 'Event Plugins' and what kind of access they have to resources like TWIG and YAML config.
The question you're REALLY asking is How do I add functionality to an application without hacking existing code ... and the answer is EventDispatcher. The rest is up to you.
There is another way to expand codebase with custom "modules": Dependency Injection Tags.
It works like this:
You create core of the app, which implements basic functionality and registers compiler pass which handles code extension
Modules can be part of the core app OR moved to external bundles
Each module must be registered in DI container with tag supported by your core app, so can be handled properly (adding menu positions, new element types as in your example)
Symfony DI compiles all services and stuff so all modules are available within app instantly (so you can for example iterate over collection of prepared items without need to dynamically building it)
In my opinion EventDispatcher is better for handling real-time actions, like sending notifications when new entity is created (e.g. controller/service dispatches event, some bundle subscribes it and send notification).
When you want to extend core I would recommend DI tags. Of course you have to build all the core (interfaces and whole architecture) so modules can extend existing services' data, then you only use your services.
Whole Symfony full-stack framework is based od this tags, so there is a lot of existing code to investigate for getting the concept.
Please let me know if it's clear enough what I wrote and what's in Symfony docs. If you need some additional info, I'll try to help.

Silverstripe with hybrid mobile app

Basically what I want to do is to create a SilverStripe CMS that allows users to enter information. Instead of displaying this information on a webpage like normal I want to output it using JSON to a hybrid mobile app. I know I have to use the RESTful server API and dataobjects but I'm a little unclear about how it will actually work.
Would appreciate a little enlightenment on this.
Saving the data into the database is very easy, this is even covered in one of the 5 basic tutorials.
Providing this information via REST isn't that hard either, if you just need basic functionality.
SilverStripe already comes with REST support, but at least in 2.4 this REST support is rather limited and really closely tied to the database model.
As far as I can tell, the REST Server has been removed in SS 3.0
Here a link to the API docs on the RestfulServer class in SS 2.4: http://api.silverstripe.org/2.4/sapphire/api/RestfulServer.html#class_details
as taken from that docs, its rather simple to allow api access to objects:
class Article extends DataObject {
static $db = array('Title'=>'Text','Published'=>'Boolean');
static $api_access = array(
'view' => array('Title'),
'edit' => array('Title'),
);
}
and than access it via http://mysite.com/api/v1/Article
If this does not cover your needs, I see 2 alternatives you could take:
Create your own Controller that outputs the rest stuff you need
Or, if you require a complex API, then use a REST framework like https://github.com/Luracast/Restler (but it might be a pain to set up Rester to work within SilverStripe because both, Restler and SS do there own URL magic)
Alternatively you can actually just spit out snippets of HTML. I have done this in several Phonegap apps. The styles, main javascript, etc are obvisouly embeded in the phonegap app. But you can create mini windows(almost like and iframe) that just get the HTML snippet. And conviently you can alter that HTML with your templates(very handy for changes).
Check out JQmobi framework and the data-defer feature.
You would just put the attribute of the data-defer to a URL on your site, and that URL is only an HTML snippet.

Migating from CakePHP to Drupal, functionality question

(I've posted this on the drupal forum too btw)
I'm converting the company websites to use Drupal, or at least trying to check that its going to be the best way forward. I have a background in PHP development, and I'm currently using the CakePHP framwork. I've built this site (not my design) and I can see how to replicate most of the functionality using Drupal, most likely using the CCK module.
http://preview.tinyurl.com/yk6u8mt
As you can see from the homepage:
A user chooses a country.
The country is passed using an ajax call to a script that decides which phone is best based on 'in country' network coverage.
A div is shown recommending the visitor the best phone for that country.
I'm wondering how to go about this in Drupal, I'm definitely not after a step by step guide, I just want to know if this kind of thing is possible with Drupal, and what approach to use.
If someone can help that would be superb. Thanks.
Okay, so you've got a path you're defining in hook_menu, which is where your form is being presented - or else you've got it set up as a webform in a node, that could work too.
Either way, in your form you're going to be using AHAH - check out http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah and http://drupal.org/node/348475 .
Basically, you're going to define another path in hook_menu that's of type MENU_CALLBACK, and which will receive the country as input, and then will return the div that you'll display on the screen.
One core example of AHAH that may be useful to you is where you're entering a password and it lets you know if the password is secure enough - check that out.
Edit: There's also some good examples at http://drupal.org/project/examples.
I would look into using CCK and views. you can set up filters for the views. If filters don't work, you have the ability to include php code. I have also successfully added jquery code in the header of a view through which I was then able to have my view filtered by what is typed in a text box.
Coming from CakePHP using Drupal is a pain in the a** - even more for developers.
It's application structure might be designed to ease extensibility but this only means you have a system to enable your own plugins and themes.
While modules are basically the M+C-part the themes are the V-part of an MVC-application. The problem is that this seperation is not very strict in Drupal - in fact you have to break it sometimes in order to make things work (e.g. you have to include a theme_mymodule_myfunction() into your module as default output which you then can override with your theme using mytheme_mymodule_myfunction() ) And don't even bother looking for classes ( see http://drupal.org/node/547518 ).
Also there is no real link from a module to a theme. On many occations this is a good thing as you can switch modules and themes seperatly without creating a problem. For application builders coming from CakePHP (or any other framework) you often feel a lack of "wholesomeness" - you create parts for a base software and have to live with it's drawbacks.
IMHO I wouldn't recommend this step. Drupal is fine if you have to manage a website and might add a few modules to add neccessary value (image gallery etc.) but I definetly don't recommend it as a base for a customized web-app.

Resources