Multiple templates in a single action

Notice: This thread is very old.
blashstar
Member | 2
+
0
-

I'm developing a site with friendly urls like that:
http://domain/<name>
The problem is that <name> can be a blog post or a section.
can i change the template inside an action?
I need invoque the blog or section template depending of the page type to render.

GEpic
Member | 562
+
0
-

I'm developing a site with friendly urls like that:

you are trying to build friendly URLs, but it's too much (and not SEO-friendly).
It's always better to use http://domain/<category/section/post>/<name>

can i change the template inside an action?

yes, you can do it with following and it calls corresponding render method:

$this->setView('blog / post / section / whatever')

be always sure that you are setting another view in action method

Last edited by GEpic (2017-02-21 00:59)

dkorpar
Member | 132
+
0
-

IMHO you are looking in wrong direction. You need to implement this in routing (determine which url run which Presenter:Action) and not switching template.

GEpic
Member | 562
+
0
-

dkorpar wrote:

IMHO you are looking in wrong direction. You need to implement this in routing (determine which url run which Presenter:Action) and not switching template.

The problem is that @blashstar wants simple route likes this for everything, then probably look up database for record with that slug with additional information what record type (blog, section, new, etc.) it is and with this information set another view.

$router[] = new Route('<slug>', 'Homepage:default');

That's really bad idea (described above) and can be done nicer, but there is a solution for what he wants.

Last edited by GEpic (2017-02-21 16:37)

blashstar
Member | 2
+
0
-

Thanks for your responses, i'm according with GEpic about the urls SEO friendly, but this is a client requirement.
With the view change, i can accomplish the functionality, thank again.

dkorpar
Member | 132
+
0
-

I got that, but then he just needs to create his route (class implementing IRouter) and there in match method define which presenter should be called. This problem has to be solved on routing level and not like this.

Last edited by dkorpar (2017-02-21 22:30)

GEpic
Member | 562
+
0
-

dkorpar wrote:

I got that, but then he just needs to create his route (class implementing IRouter) and there in match method define which presenter should be called. This problem has to be solved on routing level and not like this.

But then you must call database on routing level, cause the slugs will be the same for all the posts / pages / etc., don't know what's better atm. :/

If you have 10 static pages, then you can write them into the router directly.

But if sections / blog / etc. is created dinamically, then you have no other options.

Last edited by GEpic (2017-02-22 00:34)

mrtnzlml
Member | 140
+
+1
-

Calling database on routing level is absolutely fine and in fact I prefer that. But if you really want – there is another approach for inspiration. It's maybe three years old, but it still works in production.

It works only with this simple route rule. Nowadays I would implement it directly in the router but it may be quite complicated especially for newcomers.

dkorpar
Member | 132
+
0
-

What's the issue of calling db on routing level?
You have to call db in any way and this is routing issue so it should be solved in routing…