XF 2.1 Where can I learn about Template Macros

ibaker

Well-known member
I can edit and transform the previous XF html templates and css into exactly what I want but in v2 the basic html templates have have been "pollutted" with things called "Macros" and seems to double up the template to me. These Macros seem to interfere in using basic html when I look at a template which puts "me" right off trying to do anything as I just get so confused. So, where can I go to learn what the XF Macros in the templates do and how to use them without all the developer jargon which will turn my learning curve into a mountain...thanks for your "common English" advice
 
Not very complete though. Like this:

Code:
            <xf:macro name="custom_fields_values"
                arg-type="{$type}"
                arg-group="{$group}"
                arg-set="{$set}"
                arg-onlyInclude="{$onlyInclude}"
                arg-additionalFilters="{$additionalFilters}"
                arg-valueClass="{$valueClass}" />
No explanation of arg-type, arg-group, etc.. I need to know what arg-onlyInclude and arg-additionalFilters does, what values they expect (string? array?), etc.. I'm making do by working around it but it's a kludge. I'm trying to pull out one custom field from many in an add-on I downloaded, and not getting anywhere.
 
I'm having trouble with this too. Actually - looks like I'm in a very similar boat Wildcat Media,

Currently trying to create a custom version of

<xf:macro template="custom_fields_macros" name="custom_fields_edit" arg-type="users" arg-group="contact" arg-set="{$xf.visitor.Profile.custom_fields}" />

from the settings page, so I can split up all my custom fields into more organised sections.

I'm guessing I can use the onlyInclude argument, but it's not working with any values I put in and I can't find anything about these in the docs.

Would be nice if there was better documentation on stuff like this and filters.

Edit: Finally got part of it working after hunting around the templates for examples from elsewhere.
 
Last edited:
They do have good basic documentation on how XF works, but this sort of in-depth documentation is lacking. But I can't blame them--if they're having to write documentation for every little bit of code, that's time they can't be coding the next version. There are some good bits buried in the many threads in the forum, but it can take a while to find them (if they are even present). And part of my problem is that many years ago, I burned out on coding, and my memory is no longer what it used to be, so for me to jump in head-first to learn all this simply won't happen.

I have to cannibalize other templates myself in order to figure out how things work. Usually I just give up, after I convince myself whatever new feature I wanted really wasn't that important in the long run.
 
They do have good basic documentation on how XF works, but this sort of in-depth documentation is lacking. But I can't blame them--if they're having to write documentation for every little bit of code, that's time they can't be coding the next version. There are some good bits buried in the many threads in the forum, but it can take a while to find them (if they are even present). And part of my problem is that many years ago, I burned out on coding, and my memory is no longer what it used to be, so for me to jump in head-first to learn all this simply won't happen.

I have to cannibalize other templates myself in order to figure out how things work. Usually I just give up, after I convince myself whatever new feature I wanted really wasn't that important in the long run.
I agree. Wonder if, after almost 3 years, there’s any update on this.

Señor @Paul B, any news?
 
Anyone have any luck? I have multiple fields in each group and I'm trying to specify a single field within that group for both edit and display. Seems like the functionality is there to specify the Field ID using arguments, but like everyone else I've been unsuccessful at figuring out how to grab just one field rather than the whole group.
 
Macros aren't difficult - you can think of them as isolated sub-templates.


Let's assume you've got an array of links like this
PHP:
$links = [
    [
        'title' => 'Some cool url title'
        'url' => 'https://coolsite.com',
    ],
    [
        'title' => 'Another cool url title'
        'url' => 'https://anothersie.com',
    ],
];

and you want to output those links in a template.

There would be 3 ways to do this

Hardcoded loop within the template
maintemplate
Code:
<ul>
    <xf:foreach from="$links" value="$link">
        <li><a href="{$link.url}">{$link.title}</a></li>
    </xf:foreach>
</ul>

This is easy and works perfectly fine if this is the only place where you need this output - if you need it in multiple places you would have to duplicate code (which should be avoided)

Using a include template
maintemplate
Code:
<ul>
    <xf:foreach from="$links" value="$link">
        <xf:include template="linkbittemplate" />
    </xf:foreach>
</ul>

linkbittemplate
Code:
<li><a href="{$link.url}">{$link.title}</a></li>

This allows for code reuse, eg. linkbittemplate can be included from various templates.

But now you have saparated things and if there are many such snippets it will become chaotic quickly.
Also as the included template uses the variables from the parent scope if can only be used if the names are idempotent and you have to be careful not to created unwanted side-effects (like modifying variables from the parent)
Having many templates also has a (theoretical, probably hardly even measureable) negative runtime performance impact.

Using a macro
maintemplate
Code:
<ul>
    <xf:foreach from="$links" value="$link">
        <xf:macro id="link" arg-link="{$link}" />
    </xf:foreach>
</ul>

<xf:macro id="link" arg-link="!">
    <li><a href="{$link.url}">{$link.title}</a></li>
</xf:macro>

This way we've got only one template but also a reusable, isolated part which can be used from within the template and from other templates.

othertemplate
Code:
Here are our Top Links<br />
<br />
<ol>
    <xf:foreach from="$topLinks" value="$topLink">
        <xf:macro id="maintemplate::link" arg-link="{$topLink}" />
    </xf:foreach>
</ul>

As you can see, this other template use a different variable ($topLink) but passes this as argument name link (arg-link) to the macro where it can be used just as a "normal" template variable would be used - but isolated to this macro.

No explanation of arg-type, arg-group, etc.. I need to know what arg-onlyInclude and arg-additionalFilters does, what values they expect (string? array?)
See above, those are just the arguments for the macro - the valid types depend on the macro.

Anyone have any luck? I have multiple fields in each group and I'm trying to specify a single field within that group for both edit and display.
You may need to explain more what exactly you are trying to do - at least I don't understand your question without further explanation :(
 
Good info Kirby! I could have definitely used that when I was just starting out (like many here, I'm sure).

You may need to explain more what exactly you are trying to do - at least I don't understand your question without further explanation :(

Yeah after re-reading my post I realize it isn't exactly clear. I didn't even mention custom field macros which is what my question pertained to, and I'm probably using the wrong terms. After a bunch of trial and error (and a bit of dumb luck) I think I figured it out :)

As you know, custom fields (such as custom thread fields) are grouped by their display location (which is what I meant by group). If I have three items in the Thread status block group, the three edit fields will be shown together on forum_post_thread, and the contents of those three fields will be shown together on thread_view.

What I was looking to do is use the macro to specify one single field from the group (both for input on forum_post_thread and output on thread_view). This could be used along with conditionals for far greater flexibility than displaying the entire custom field group, especially if the custom fields for a specific content type/addon include a self_place display location as some do.

No explanation of arg-type, arg-group, etc.. I need to know what arg-onlyInclude and arg-additionalFilters does, what values they expect (string? array?), etc.. I'm making do by working around it but it's a kludge. I'm trying to pull out one custom field from many in an add-on I downloaded, and not getting anywhere.

I had a feeling these one of these two args could possibly be used to do this, but I couldn't find any examples of it in existing code and documentation is non-existent. Turns out my thought was correct, it can be done like this: arg-onlyInclude="{{ ['<custom_field_id>'] }}".

Here's a complete example:

Code:
                    <xf:macro id="custom_fields_macros::custom_fields_view"
                        arg-type="threads"
                        arg-group="thread_status"
                        arg-onlyInclude="{$forum.field_cache}"
                        arg-set="{$thread.custom_fields}"
                        arg-wrapperClass="blockStatus-message"
                        arg-onlyInclude="{{ ['<custom_field_id>'] }}" />

I'm sure someone with actual programming knowledge could have looked at the PHP and figured this out in minutes (that ain't me :ROFLMAO:).

I've tried to figure this out many times over the years, so I figured I'd post a detailed explanation for others who may be looking.
 
Last edited:
Back
Top Bottom