Re: [habari-dev] Generating a URL to display a template

42 views
Skip to first unread message

Christopher Davis

unread,
Apr 28, 2013, 8:54:09 AM4/28/13
to habar...@googlegroups.com
Hey Thomas.  Try calling theme_route_display_blog($theme) {
       $theme->display('blog');
}

Also you might want to call the rewrite rule filter to add your custom rule, instead of adding it via action_init. 

Sent from my iPhone

On Apr 28, 2013, at 7:21 AM, Nathan Thomas <afterno...@googlemail.com> wrote:

Hi all,

I've really enjoyed using Habari over the past couple of years, and the transparency of the code has encouraged me to try to get to grips with it a little more.

At the moment I'm trying to figure out how to display a custom template when I click on a link. I managed to create the new template using add_template, and it shows up when I call $theme->display('<new template>'). However, when I tried to create a rewrite rule to display the template whenever a specific URL is requested, I couldn't get it to work.

Here's the code:

<code>
public function action_init() {
 $this->add_template('blog', dirname(__FILE__).'/blog.php',false);
        $this->add_rule('"blog"','display_blog');
    }

    public function action_handler_display_blog( $handler )
    {
        $handler->$theme->display('blog');
    }
</code>

From what I understand, this should add a new rule that uses the display_blog handler to display the blog.php template for the URL '<siteurl>/blog', but I just get a 404.

Where am I going wrong?

Thanks,
Nathan

P.S. The new Habari site design looks great BTW!

--
--
To post to this group, send email to habar...@googlegroups.com
To unsubscribe from this group, send email to habari-dev-...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/habari-dev
---
You received this message because you are subscribed to the Google Groups "habari-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to habari-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Owen Winkler

unread,
Apr 28, 2013, 8:57:37 AM4/28/13
to habar...@googlegroups.com
A simpler method for what you're trying to accomplish:

public function action_init() {
$this->add_template('blog', dirname(__FILE__).'/blog.php');
$this->add_rule('"blog"','display_blog');
}

public function theme_route_display_blog( $theme, $url_params ) {
$theme->display('blog');
}

This is new in 0.9.1 and very handy. It circumvents the whole
"action_handler" system, so you'll be required to put all of your own
variables into the theme (doesn't sound like it'll be a problem for your
use), but it will directly output some template for a specific request.

If you're ambitious, you could even do this, which does the same thing:

public function action_init() {
$this->add_template('blog', dirname(__FILE__).'/blog.php');
$this->add_rule(
'"blog"',
'display_blog',
function ( $theme, $url_params ) {
$theme->display('blog');
}
);
}

Hope that helps.

Owen

Christopher Davis

unread,
Apr 28, 2013, 4:00:48 PM4/28/13
to habar...@googlegroups.com
Hm, I would call tho from a plugin. 

Sent from my iPhone

On Apr 28, 2013, at 11:23 AM, Nathan Thomas <afterno...@googlemail.com> wrote:

Thanks Owen and Chris, I've tried your suggestions but I still get the 404. Just to check: this code should go in the custom theme class in theme.php, right? Is there any way to check what is returned by the add_rule?

Chris Meller

unread,
Apr 28, 2013, 5:57:07 PM4/28/13
to Habari Dev
It's theme related, so depending on exactly what it does, I'd probably be inclined to stick it in the theme too. It should work in either place, but tossing it in a plugin might be worth a try, just to make sure.

Owen Winkler

unread,
Apr 28, 2013, 6:29:08 PM4/28/13
to habar...@googlegroups.com
On 4/28/2013 5:57 PM, Chris Meller wrote:
> It's theme related, so depending on exactly what it does, I'd probably
> be inclined to stick it in the theme too. It should work in either
> place, but tossing it in a plugin might be worth a try, just to make sure.

It's not possible to create new routes from a theme because Habari
doesn't instantiate the theme class until after it knows that it needs
to route to one. For example, the Atom feed URL does not instantiate
the theme because the theme is not needed for the Atom feed's construction.

Features that require additional URLs should not be added via a theme,
but instead from a plugin. If your theme requires a feature (such as a
new route) that a plugin provides, it can require the feature using the
features defined in the theme XML.

Owen

Matt Read

unread,
Apr 29, 2013, 9:16:42 AM4/29/13
to habar...@googlegroups.com
If you want add rewrite rules in a theme you need to insert them in the
database. for example, in the conquistador theme it adds a rule on
activation (action_theme_activated) like so:
https://github.com/MattRead/conquistador/blob/master/theme.php#L463

with handler of 'UserThemeHandler' and action 'display_archives' it will
call 'act_display_archives' in the theme class.
https://github.com/MattRead/conquistador/blob/master/theme.php#L480
Reply all
Reply to author
Forward
Message has been deleted
0 new messages