Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between 2 or more templates with an action in controllers?

I have a default Phoenix application. This app will have a page_controller which will load an index.html.eex file.

The app will know to use the view to access templates/page/index.html.eex.

Now say you have created another html page which is identical to index.html.eex in every way except it is in French.

As we do not want to create a whole new Phoenix application which will have all the same code, with the exception being the French translation of the current page/index.html.eex, is there a way to tell the view or the controller which file needs to be loaded.

Is there a plug which can be placed in the router to alter where render will look for it's templates?

like image 830
RobStallion Avatar asked Jul 28 '17 19:07

RobStallion


People also ask

Can there be the same action method name with the same Httpverb in a Controller?

The methods must have different parameters. This is dictated by the programming language and is basically method overloading. Even if two methods have different parameters, they must also have different http verb attributes (httpget, httppost).

What is route Controller?

Routing controllers allow you to create the controller classes with methods used to handle the requests. Now, we will understand the routing controllers through an example. Step 1: First, we need to create a controller. We already created the controller named as 'PostController' in the previous topic.

What is difference between attribute and conventional routing?

As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.


1 Answers

First of all I would suggest you to use Gettext to use labels for French pages.

For example you can all French templates keep in the very same folders (to don't change logic for view), but to name them with suffix eg. "index_fr.html.eex" etc. and then you can write quite simple helper (not necessarily a plug) that will add to all of your templates this suffix.

Still, I would recommend you using Gettext - template's source code is only in place and almost all of the logic Gettext handles for you.

like image 166
PatNowak Avatar answered Oct 16 '22 16:10

PatNowak