pods-frontier
pods-frontier copied to clipboard
Corrections on command tags
@Shelob9 I Was working from memory last night about some functionality. I went over it this morning to fix and confirm things so here they are.
[each] This is a loop on a relation. If books has a relation to chapters then you would do:
[each chapters]
<p>{@chapters.post_title}</p>
[/each]
Inside the each the tags are context based meaning you don't need the chapters part. and can use just {@post_title} but then you loose autocomplete. I have the code remove the prefixes before render to allow keeping them in and retaining autocomplete functionality.
[before] & [after] I was a little mistaken on this. Its not run before and after each loop, but rather before and after the template as a whole. A template is looped for each entry the whole template has. The before and after are used to wrap all the entries. example:
[before] <ul class="books-wrapper"> [/before]
<li>{@post_title}</li>
[after] </ul> [/after]
To achieve what I originally thought it was is simply done with an [if]. example:
[if chapters]
<ul>
[each chapters]
<li>{@chapters.post_title}</li>
[/each]
</ul>
[else]
<p>Sorry, No chapters</p>
[/if]
[once] The once tag is context based. meaning you can use it once per loop. example:
[if chapters]
<ul>
[each chapters]
<li [once]class="first-chapter"[/once]>{@chapters.post_title}</li>
[/each]
</ul>
[/if]
The first chapter will have the .first-chapter class for each template entry. So if this template is against all books, the each books first chapter will have the .first-chapter class.
So for a final example covering all the above:
[before]<div class="books-list">[/before]
<h3>{@name}</h3>
[if books]
<h5>Books</h5>
<ul>
[each books]
<li>{@post_title}
[if cover_images]
<ul>
[each cover_images]
<li class="cover-image[once] first-image[/once]">{@_img.thumbnail}</li>
[/each]
</ul>
[else]
<p class="no-images">No Images for this book</p>
[/if]
</li>
[/each]
</ul>
[else]
<p>Author has no books uploaded</p>
[/if]
[after]</div>[/after]
This looks pretty cool!
I only have the concern that we're using the WP shortcode syntax when there could be possible conflicts from shortcodes of the same names. I wonder if we used the syntax {{.....}} instead, and did our own parsing of those, either via a forked or specialized version of what shortcodes do in their parsing function.
On a technical aspect, I pre-filter the tags and replace them with the true shortcodes, which are:
- pod_sub_template
- pod_once_template
- pod_after_template
- pod_before_template
- pod_if_field
They have more complex internal functionality so you can just use them as is. specifically since WP shortcodes cannot be nested:
[shortcode] [shortcode] [/shortcode] [/shortcode]
This will break in normal use. If you look at my last example, I have [if] within an [if] as well as [each] within an [each]
@Desertsnowman Thank you for the corrections. I am working on updating the reamde, which is coming together nicely. What would be an example use for the before and after tags? Not sure what's the functional difference between using them, and just putting something at the begging and end of the template.
@Shelob9 Lets say you are listing multiple Pod items using a shortcode like this: [pods name="book" template="Books"].
Now if you want the book list to be in a unordered list, you'll need some way to have the <ul> and </ul> start and end correctly. Using shortcodes is fairly simple as you can place the tags in the editor like:
Template:
<li>{@post_title}</li>
Page Content:
<ul>[pods name="book" template="Books"]</ul>
This is all fine, but you can't rely on the content to form part of the template. You also cant use it in the layout builder since you cant place html between template elements. Placing the <ul> in the template like:
<ul>
<li>{@post_title}</li>
</ul>
Will result in:
<ul>
<li>Book One</li>
</ul>
<ul>
<li>Book Two</li>
</ul>
<ul>
<li>Book Three</li>
</ul>
So this is where the before & after come in. In the template you can do:
[before]<ul>[/before]
<li>{@post_title}</li>
[after]</ul>[/after]
Which will result in:
<ul>
<li>Book One</li>
<li>Book Two</li>
<li>Book Three</li>
</ul>
@Desertsnowman Thanks, that makes sense. Before runs before all iterations, after runs after all iterations, when the template is called in a context that calls it to loop.
I will get the readme finished tonight or tomorrow.