Jump to content

How do we use pagestack?


Zahari M.
 Share

Recommended Posts

Hi guys!

Another question for you super smart guys!

How exactly do we use pagestack?

Here is what Im doing and trying to do...

Lets say I have an article template and am viewing an article as a page. All is good here.

Lets say I have a video template. Each page of this video template holds the markup about one video on youtube. I can view each page here and all i see is a page title and the video. All is good here.

So, what I am doing is I am using a page reference field in my article template so that I can select and render a video inside my articles.

So.... within the markup of my article template, conceptually I am calling in a video into it like this....

echo $pages->get(1169)->render('./markup/pages/pg_Video_Element.php');

This works great!

Now.... what I would like to do is this... I would like the video template to be able to retrieve and echo out the article pages that are "requesting"the video page.

Unfortunately there are only a few mentions of pagestack, but there is not a worked out example that shows how we actually implement it!

Would it be something like echo $pages->get(1169)->render('./lib/markup/foundation/pages/pg_Video_Element.php', ['pageStack']);

Or something else?
 

I would be really grateful if someone could show me what would the correct syntax or way of writing in pagestack into our render request as an option.... and how we might write out an expression to retrieve and use it on the other end!

Cheers guys!

Link to comment
Share on other sites

Hi Zahari,

I haven't used pagestack before, but from what I just read, you don't send it with the render call. You would use: $options['pagestack'] in your pg_Video_Element.php template file. It is an array of the pages that rendered the template so you would foreach through: $options['pagestack'] to get the article page.

Let me know if that works. Maybe I should go test it too :)

  • Like 1
Link to comment
Share on other sites

Forgot to mention, in case you don't know you can send your own variables, arrays to the template file using render. eg:

echo $pages->get(1169)->render('./lib/markup/foundation/pages/pg_Video_Element.php', array('myVar' => $myVar, 'myArray' => $myArray);

Then retrieve them in your pg_Video_Element.php with $options['myVar'];

Link to comment
Share on other sites

Hi Adrian and Soma

Thanks for the replies. Still not having much luck.. but I'm not very good at this at all...

Anyhow guys....

For fear of any scoping related issues, I have cleaned up my video template, and inserted all my html markup for the video directly inside the video template.

As you suggested Adrian, I added this to my video template:

$requesters = $options['pagestack'];

$out .= "<ul>";
foreach ($requesters as $requestee) {$out .= "<li><a href='{$requestee->url}'>{$requestee->title}</a></li>";}
$out .= "</ul>";

What happens now when I view the article containing the video, or go to the video page directly, I get this error:

Notice: Undefined index: pagestack in /Applications/MAMP/htdocs/test-site.dev/site/templates/t_Video.php on line 66
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/blog-starter.dev/site/templates/t_Video.php on line 69

Any ideas what it is that I am doing wrong?

Cheers guys!

Link to comment
Share on other sites

Hi Soma & Adrian

Thanks so much for your replies

Ok guys.... thats fixed the error!

Now that I have done this, what I see happening is this...

On my video page, I see the video

On the article page, I see the article, the video and the page that requested the video.

I want to see the page that requested the video on the video page. But instead it is showing it on the article page which is kinda pointless!!

Any ideas how to display this information on the video page?

Cheers!!!

Link to comment
Share on other sites

If I am understanding you correctly (which I am not sure I am), then I don't think pageStack is going to help you here. From your video page you need to do a search through the page field that is in your articles template and look references to the video that is currently being displayed. Does that sound right?

Link to comment
Share on other sites

Hi Adrian

Thanks for your reply. Much appreciated.

So I have a video template.

So lets say I am viewing a page using this template. I would see a video.

What I am after is that underneath the video it would say.... this video is used by article/3, article/100 etc.

I would like to know which article page rendered my video page :)

There was a thread where someone asked something similar....

http://processwire.com/talk/topic/3660-what-page-was-the-caller-of-render/

In this topic fellow member wanze said this:

  1. Use the dev version of Pw and get the page that rendered the template with $options['pageStack'].
  2. The method ryan mentioned in the post at the end. Before rendering the home template: $home->caller = $page; Then in your home template, you can access the caller page with $page->caller;

I guess this references a post by Ryan here:

http://processwire.com/talk/topic/3145-multiple-views-for-templates/page-2

In it Ryan says...

One other addition that I'm thinking people might like is
$options['pageStack']. That is an array containing a stack of pages that
called render(). So if you are doing any recursive rendering of pages,
any template can access $options['pageStack'] to see what page is
rendering it, and any others before it. Previously this was not
possible, and the only way a template could tell what other page was
rendering it (if any) was for that renderer to tell the renderee, via
$mypage->caller = $page; or something like that.

So Adrian, I thought, I guess mistakenly? that I could use pageStack in my video template to see which pages were rendering it...

Does it make sense what I'm trying to do?

Cheers

Link to comment
Share on other sites

Are you talking about articles that are rendering at the moment you want to query this, or just all the articles that have the currently viewed video selected in the video page field on the article's page?

I am still not convinced  I know what you want - sorry, but if the latter, then something like this would do it:

foreach($pages->get("template=articles,video=$video_id") as $article){
    echo "<li>{$article->title}</li>";
}
Link to comment
Share on other sites

Hi Adrian

Very appreciative for your looking at this :)

Before your last reply, your previous one gave me the hint I needed....

Thanks to you it dawned on me that on say video page id = 3, I could select for pages that have a video selection field whose id = 3. This would tell me which page or pages have "used" or "contain" this video.

And in your latest reply you have shown me how to do it!!

Thank you thank you! :) :)

So my problem is solved.

BUT....

Just for discussions sake now.... as there is no problem anymore...

My understanding was that pageStack could be used to end up to do exactly what you have suggested above, i.e. I assumed that it could be used to generate the list of article page/s that were rendering a particular video page.

So, I thought that pageStack was an alternative way of generating the very list that your code above would generate.

I did not think of doing it the way you suggested earlier. But I'm glad you showed me it as I can see that that concept is a great one!

I guess I made a wrong assumption here about pageStack.

But all is not lost. pageStack has come in useful today! In my case, I've been using template classes within template files to enable me to reference external markup files to sort of lay things out in my own (amateurish) MVC layout....

Thanks to pageStack I can now get away with using one video markup file for the two cases its needed. On the article page we just get the central portion of the markup that deals with displaying the video, and on the video page, we can add our header markup and with the code you gave me, the markup where the video is being used

Something like this

// In Video Template
// We use pageStack here to test if the video is being rendered via its template video or in an article page.
if (!count($options['pageStack'])) {
$page->set('requester', 't_Video'); // this will resolve on video template pages only, not article pages.	
}

// In External Video Markup File
if ($page->requester == "t_Video") { // Displayed only if we are looking at it on one of our video pages along with it's native t_Video template.

All basic stuff for you guys Im sure...

Big step for me thou...

Anyways thanks Adrian and Soma!

  • Like 1
Link to comment
Share on other sites

Glad you got it all working as you wanted.

Remember we are all learning everyday, whether it is something new in PW or learning something else in another code language that I haven't needed to do before. It's part of the fun and the frustration :)

Link to comment
Share on other sites

Hi Adrian

Thanks again. :)

Yeah. ProcessWire is such immense fun!

BTW, for anyone reading this in the future... just needed to change $pages->get to $pages->find

This got me the pages that were using the video...

foreach($pages->find("video_selector=$page->id") as $article){echo "<li>{$article->title}</li>";}

Just as an aside Adrian, you might be interested in watching this video I did. Video is the next thing I need to practise on. Once I have gotten my website sorted, I can spend some more time working on taking videos. The concept here is mix still photos with video clips. Totally unscripted and on the fly.... >:D

Why watch it? 2 reasons. Nice car. Shot with a handphone and the first video on it at that. :D

https://www.youtube.com/watch?v=gYM8cPQRE8k

My sites really coming along well. Can't wait to add your awesome video thumbs module in soon. All hail Adrian!!!!

Cheers guys



Oh! I didnt realise the video will show up here.

I didnt mean to spam the site. Just thought it would show up as a link

  • Like 1
Link to comment
Share on other sites

@Zahari:  nice video, nice car. :)

Maybe you find this interesting for some stills in your videos: http://www.photofilmstrip.org/1-1-Home.html

I have used it here on this old (and private) site where a 6 year old child want to present his new built time machine.

(the video is the sepia one)

Edited by horst
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...