Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Outputting a variable that is defined further down the page?
I have a site with a series of longform text articles written by different authors. Each author has their own profile article on a pageless section containing a biography in different lengths, some contact details and their article image.
I want to output the author’s name under the heading but have the author’s biographic details at the end of the article. Remembering this thread, I thought I could do something clever and retrieve the author information at the bottom of the page where I want it, and then post-populate the author name at the top via post-processed variable. It didn’t work but I’m not sure why. This is the basic principle:
<txp:article>
<!-- title block in page_header form -->
<txp:variable[-1] name="page_author" wraptag="p" class="page-author" />
<!-- rest of article -->
<txp:output_form form="author_profile" />
</txp:article>
and the author_profile form has:
<txp:if_individual_article>
<txp:if_custom_field name="author_article_id">
<txp:article_custom id='<txp:custom_field name="author_article_id" />' wraptag="div" class="author-biography">
<txp:images id='<txp:custom_field name="article_image" />' limit="1">
<img src="<txp:image_url />" class="author-img" alt="<txp:image_info type="alt" />" width="<txp:image_info type="w" />" height="<txp:image_info type="h" />">
<txp:jcr_image_custom name="copyright" variable="author_img_credit" />
</txp:images>
<div class="author-desc">
<!-- this is where the variable is defined -->
<txp:variable name="page_author"><txp:title /></txp:variable>
<txp:variable name="page_author" wraptag="h3" class="author-name" />
<txp:body />
<txp:variable name="author_img_credit" wraptag='<p class="author-photo-credit">Photo: <+></p>' />
</div>
</txp:article_custom>
</txp:if_custom_field>
</txp:if_individual_article>
The author profile outputs perfectly, so that is all working but the author_name at the top does not. The tag trace shows:
<txp:variable[-1] name="page_author" wraptag="p" class="page-author" />
[<txp:variable>: Unknown variable 'page_author']
Any ideas what I am doing wrong?
(I know I could call the article_custom bit twice, or I could get the author details at the top of the page, store them in variables and output them lower down)
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting a variable that is defined further down the page?
I expect it’s the among its txp siblings that’s catching you out. The output_form tag is the same level as the variable tag you want to output, but then the form goes “deeper” to set the value of the variable so the variable tag that outputs the page_author can’t “see” inside the form.
I might be way off, so please wait for Oleg to chime in. Perhaps -1 has special meaning? But I expect if you set the variable directly inside your article tag, towards the bottom (just as a test) it will show up because the tags are then siblings.
Perhaps varying the value in the square brackets to match the txp tag nesting depth that your variable assignment is done (4? 5? -4? -5?) will work? (I honestly don’t know as I’ve not delved into this much).
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: Outputting a variable that is defined further down the page?
Good tip. I guess I still don’t properly understand what among its txp siblings means.
You’re right. If I output the variable[-1] “alongside” but above the output_form tag, it works.
But putting it back where it belongs and varying the number didn’t help, unfortunately.
My site is a bit more complex than my pseudo code above:
<txp:if_article_list>
<!-- list stuff -->
<txp:else />
<txp:output_form form="page_header" /> <!-- txp:variable[-x] tag in here -->
<txp:output_form form="page_body" /> <!-- txp:variable defined in author_profile form in here -->
</txp:if_article_list>
And then there’s an additional article_custom in the author_profile form!!
Do perhaps the tag trace indentations provide any clues?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting a variable that is defined further down the page?
jakob wrote #342335:
putting it back where it belongs and varying the number didn’t help, unfortunately.
Drat. Was a long shot. Hopefully Oleg can swagger in with some insight because…
jakob wrote #342335:
I still don’t properly understand what among its txp siblings means.
(you and me both) :)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: Outputting a variable that is defined further down the page?
For the moment I resorted to defining variables at the page head and then using them later. I’d still like to understand what I was trying to do, though.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting a variable that is defined further down the page?
Among its siblings means that
<txp:hide process="1">
<txp:variable[-1] name="test" />
<txp:variable name="test" value="me" />
</txp:hide>
works as expected, but
<txp:hide process="1">
<txp:variable[-1] name="test" />
</txp:hide>
<txp:hide process="1">
<txp:variable name="test" value="me" />
</txp:hide>
does not. Your block should be
<txp:if_article_list>
<!-- list stuff -->
<txp:else />
<txp:output_form[-1] form="page_header" /> <!-- txp:variable tag in output here -->
<txp:output_form form="page_body" /> <!-- txp:variable defined in author_profile form in here -->
</txp:if_article_list>
Offline
Re: Outputting a variable that is defined further down the page?
Thank you for the explanation (and sorry for not replying and thanking earlier).
That said, I still don’t think I have a very good grasp of the concept.
I came up against a similar situation and thought I’d try out the principle again. This is the basic scheme for the home / default landing page:
----------------------------------------
“Hero” container
-- hero image (from variable defined later)
-- welcome intro text (from variable defined later)
----------------------------------------
Links to sections of the site
----------------------------------------
Welcome text from a pending article (variables defined here)
----------------------------------------
My intention was to store the home page blurb in an article that otherwise doesn’t show on the site. Here I used a “pending” article rather than an article in a pageless section (it seems pending can be referenced via status="3" but hidden and draft didn’t want to).
With much experimentation, I did get it working, but it wasn’t quite how I expected:
<section class="home-hero">
<!-- welcome tagline -->
<txp:variable[-1] name="welcome_tagline" wraptag="h2" />
<!-- welcome image -->
<txp:images[-1] id='<txp:variable name="welcome_img_id" />'>
...
</txp:images>
</section>
<!-- links block here -->
<!-- welcome text -->
<txp:article_custom id="123" status="3" wraptag="section" class="introduction">
<txp:body wraptag="div" class="content" />
<!-- variables for use further up the page -->
<txp:excerpt escape="p" variable="welcome_tagline" />
<txp:custom_field name="article_image" variable="welcome_img_id" />
</txp:article_custom>
The heading was quite straightforward but the article_image had me flummoxed until I did txp:images[-1] rather than txp:variable[-1] for the tag-in-tag attribute. I’m still trying to picture the concept in my mind 🤪 so I can use this principle more predictably 🥳.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting a variable that is defined further down the page?
A silly question: what is the purpose of the suffix [-1]?
Is it used to defer?
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: Outputting a variable that is defined further down the page?
Kind of. It effectively changes the processing order (or context/scope). Maybe someone else can provide a more precise way of expressing it?
The link in my original post shows a way of providing a JSON-LD block in the page head and then populating the items with variables that are defined later on down the page. Previously we’ve had to include article(_custom) twice on a page or use something like arc_meta. I’m not sure how well my original json-ld example holds up with more complex page and form structures, and that’s what I’d like to understand better.
TXP Builders – finely-crafted code, design and txp
Offline