Aloha!
> Well, I finally dipped my toes into the world of off-the-shelf content
> management systems, and Habari was my first choice due to SQLite
> support. The first thing I'm going to do with it is write my own
> theme, but I'm finding it extremely frustrating, and I think this may
> come from my lack of previous OO-PHP experience.
Welcome to the community! We'll do the best we can to help you out.
> What's got me confused is the use of predefined variables and their
> member functions when writing the dynamic portions of templates (posts
> and comments home.php, for example). I'm working from an existing
> theme (charcoal, as it happens), and stripping this down into a bare-
> bones HTML layout.
Habari objects implement several "magic" functions like __get() and __set():
http://us3.php.net/__get
> So, at first I was willing to ignore how this magically works, but the
> next one I found was $post->title_out, and I have no idea where it
> comes from. My understanding is that $post is an object created from
> the Post class, but doesn't that mean that I should see a member
> variable or function by the name 'title_out' in system/classes/
> post.php? I know that if I change $post->title_out to $post->title,
> the result is exactly the same; I'm just really confused how PHP and/
> or Habari is parsing this code.
The __get() method intercepts requests for object properties and
allows us to execute methods to return things as we want them. One of
the benefits of this is the use of "virtual" properties. As you've
discovered, there is no title_out property defined in the Post class.
Instead, requests for title_out get sent to Post::__get(), which
implements the filter logic.
Take a look at the first couple lines of Post::__get(). Inside the
first if block we check if the requested property matches some portion
of an actual property, and then check for the existence of an
underscore on the requested property. If that's true, we know we're
asking for a filtered version of an actual property.
So we separate out the actual property name and the filter. In this
case, we want the "out" filter on the "title" property. We don't have
a specific case for "title" in the switch statement, so it falls
through to the default case, fetching the value of "title" from the
parent QueryRecord's method to return the field value of that name.
After invoking general plugins on the requested property ("title"), we
invoke specific filters for the specified property ("title_out"). If
no filters exist, then the return value of "title" and "title_out"
will be the same, as you've discovered.
Inside your theme.php file, you could create a filter called
"post_title_out" that could change the output of the title. You could
make it all uppercase, for example, or manipulate the title in some
other clever way.
Hopefully with this information you can figure out $post->tags_out on
your own. if not, let us know. :)
Cheers,
Scott
It's opaque in some situations, but provides a tremendous amount of
very useful functionality without overly bloating or obfuscating the
underlying code. It allows themes to leverage custom filters defined
in their theme.php file without requiring additional plugins, for
example.
> I'd just like to clarify one or your statements... "...you could
> create a filter called post_title_out...".
>
> Does that mean: public function post_title_out(){...}
> or perhaps: public function filter_post_title_out(){...}
> or something else?
public function filter_post_title_out(). Sorry about the confusing
reply on my part.
> In user/themes/charcoal/home.php, there is a call to $theme-
> >post_comments_link($post, 'No Comments', '%s Comment', '%s Comments')
> which has got me stumped, and I really hope there's a simple answer to
> this. The main query that started me off is that the call in home.php
> isn't preceded by 'echo', implying that the function itself does the
> echoing. I spotted charcoal::theme_post_comments_link($theme, $post,
> $zero, $one, $more) in user/themes/charcoal/theme.php, and due to the
> slightly different function name, I presume that it's made its way
> here through one or two __get() handlers. So I had a look around, and
> I figured that Theme::_get() redirects to RawPHPEngine::_get(), and
> from there the "post_comments_link" string should match one of the
> elements in the engine_vars[] array. I guess my assumptions are
> incorrect, because I can't find any other reference to
> "post_comments_link" in any files, and this doesn't quite explain how
> we get back to charcoal::theme_post_comments_link().
There also exists some magic theme functions which I haven't explored.
Themeing isn't my itch, so I don't scratch it. Hopefully someone
else will explain those bits. It might be that $theme-> methods
automatically output stuff for you.
Cheers,
Scott