Page.find query

Hi

In a twig I have the below and it outputs the page title+link.

{% for p in page.find('/wallpapers').children if p != page %}
<a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}

I have another variable for the image artistimg I have added this to the twig like below but the variable name doesn’t get outputted. I also tried changing this to page.artistimg but no name is added. I also tried changing p.title to p.folder (another variable in those md files) and nothing gets returned. So am I correct to say that page.find doesn’t work with any other variable other then page title

{% for p in page.find('/wallpapers').children if p != page %}
{{p.artistimg}} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}

I also tried the below to try and dump the image out using the variable but again nothing.

{% for p in page.find('/wallpapers').children if p != page %}
{{ dump(pages.find('/images/landing-page-thumbnails/'~page.header.artisti mg).media.images) }} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}

Is what I am trying to do possible?

If I understand correctly this artistimg variable is a header variable of your children pages? In that case, you’d use:

—twig
{{ p.header.artistimg }}


to get its value. What you're trying to do next seems like it should work to me, apart from the typo (you have a space in `artisti mg`) and the fact that it should be `p.header` instead of `page.header`, providing your folder structure looks like:

user/
pages/
images/
landing-page-thumbnails/
[artistimg-value]/
some-image.jpg

Hi

Ah I can see where I was going wrong. I have managed to out this variable onto the page so it can be seen but still cant seem to get the image to appear. I have slightly modified the code based on your table code.

	{% for p in page.find('/wallpapers').children if p != page %}
{{ dump(pages.find('/images/landing-page-thumbnails/').media[p.header.artistimg~'.jpg']) }} {{p.header.artistimg}} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}

folder structure is like

user/
  pages/
    images/
      landing-page-thumbnails/
        [artistimg-value].jpg
---

I think this should do the trick:

—twig
{% for p in page.find(’/wallpapers’).children if p != page %}
{{ dump(pages.find(’/images/landing-page- thumbnails’).media.images[p.header.artistimg~’.jpg’]) }} {{p.header.artistimg}} {{ p.title}}
{% endfor %}


Note I removed the slash after `landing-page-thumbnails` and added `.images` behind `.media`

An easy way to find out where you're going wrong is to make your dump less specific and drill in step by step :) i.e. I start with `dump(pages.find('/images/landing-page-thumbnails/'))` and you'll notice it does not find the page with that trailing slash.

Hi

Ah I see will note that little trick down :slight_smile:

Awesome got it work :slight_smile: thanks for the help :slight_smile:

Any idea on how to make the front end display better? I have added in a table

<table>
  <tr>
    <th>1</th>
    <th>2</th>
	<th>3</th>
	<th>4</th>
  </tr>
  <tr>
      <td>{% for p in page.find('/wallpapers').children if p != page %}
   {{ pages.find('/images/landing-page-thumbnails').media.images[p.header.artistimg~'.jpg'] }} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}</td>
  </tr>
</table> 

So everything appears under “1” in one column. I cant figure out how to make the 2nd image/link appear in column 2, next in column 3, next in column 4 before it starts over again in column 1.

How about this?

<table>
  <tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
    <th>4</th>
  </tr>
  <tr>
  {% for p in page.find('/wallpapers').children if p != page %}
      {% if loop.index0 > 0 and loop.index0 is divisible by(4) %}
      </tr><tr>
      {% endif %}
      <td>
        {{ pages.find('/images/landing-page-thumbnails').media.images[p.header.artistimg~'.jpg'] }} <a href="{{p.url}}/">{{ p.title}}</a>
      </td>
  {% endfor %}
  </tr>
</table>
--- 

Twig links:
- Divisble by: http://twig.sensiolabs.org/doc/tests/divisibleby.html
- Loop.index0: http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

WOW, your a genius :smiley:

That worked a treat :smiley: I only just found out about the page.find working for child folders. I had manually created about 600 pages with hard coded children folder links. Will need to go over those again to make them work in this new method and delete the hardcoded table/links and add in a variable for the image but at least if any more get added to that section they will auto appear as opposed to manually being done.

Least my new section with about the double number of pages is pretty much complete thanks to you in one day :smiley: (other manual one took me like over 2 weeks @ several hours a day).

I was just looking at Batch until I refreshed forum and you had already answered :slight_smile:

Thank you very much Gert :slight_smile:

Hmmm one section has sub childs too.

user/
  pages/
    01.home
    02.wallpapers
         child1
         child2
    03.videos
         child1
                  subchild-1
                  subchild-2
         child2
                  subchild-1
                  subchild-2

So something like the above. The table code work’s for wallpapers and works for the videos top level child’s. What I cant figure out now is how to make child1 table output its sub childs.

{% for p in page.find('/videos/'~page.header.folder~'-videos/').children if p != page %}

The child1 already contains a folder variable that matches its name inside videos only thing its missing is the -videos aspect. I’m thinking with the code the twig is’nt going into the child just yet in order to pickout that variable (??!!!)

Can you provide a more complete version of your twig? Maybe using a gist? Having a bit of trouble understanding what exactly you’re doing here. :slight_smile:

Hi

Gist added so thats the twig. Basically with my site structure I have the parent folder, which has sub folders (which has sub folders).

01.home
02.wallpapers
      artist-1
      artist-2
      etc.....
03.videos
      artist-name-1-videos
              artist-video-1
              artist-video-2
              artist-video-3
      artist-videos-2-videos
              artist-video-4
              artist-video-5

So sites folder structure is like that. for 02.wallpapers and 03.videos (top level) the original code is working. Now I am trying the same auto folder outputting at the artist-name-1 level (sub folder of parent) but the code points to only the parent folder so I was trying to add a variable here so it finds the sub folder…

{% for p in page.find('/videos/'~page.header.folder~'-videos/').children if p != page %} 

full twig of above snippet code

So with that I was expecting the page artist-name-1-videos to put into a table
artist-video-1 | artist-video-2 | artist-video-3

Browsing to the page artist-videos-2-videos I would be expecting
artist-video-4 | artist-video-5

and so on.

If you only want the children of the current page, so when you are in artist-name-1-videos and you want to get the following pages: [ artist-video-1, artist-video-2, artist-video-3 ], you don’t need to use page.find, you can simply do:

—twig
{% for p in page.children %}


to loop through the children of the current page. Is thatt what you are looking for?

Ah I see yup that worked LOL

Awesome all done :slight_smile: :smiley:

Just need to tidy up the .md files to strip out the existing tables.

Thanks a lot for your help really appreciated going to save me loads of time now :smiley:

Me again :frowning: lol

Do you know if anything can be done with empty columns? So let’s say the section is videos… for the particular artist I only have one video. The table outputs one row/column and the other 3 columns are blank and it looks a bit daft. Anyway to fill those columns (with something hard coded or just blank empty columns).

Image of Table

So that’s the result just wondering if anything can be done with that? Would not mind having hardcoded td’s to appear only if table needs it (so would have 3 max) and 1st instance all 3 would appear in 2nd one only 2 would appear and 3rd only 1 and 4th none as table is complete.

The batch filter in twig allows you to pass a second parameter that specifies how to fill ‘empty’ columns. http://twig.sensiolabs.org/doc/filters/batch.html
Might be a better alternative than the for loop with is divisible by I mentioned earlier.