Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Page.find query

Started by Muut Archive 11 years ago · 13 replies · 642 views
11 years ago

Hi

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

TWIG
{% 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

TWIG
{% 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.

TWIG
{% 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?

11 years ago

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 }}

TXT

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

11 years ago

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.

TWIG
    {% 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

TXT

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

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}} <a href="{{p.url}}/">{{ p.title}}</a>
{% endfor %}

TXT


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.
11 years ago

Hi

Ah I see will note that little trick down :)

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

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

TWIG
<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.

11 years ago

How about this?

TWIG

<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
11 years ago

WOW, your a genius :D

That worked a treat :D 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 :D (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 :)

Thank you very much Gert :)

11 years ago

Hmmm one section has sub childs too.

TXT
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.

TWIG
{% 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 (??!!!)

11 years ago

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. :)

11 years ago

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).

TXT
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...

TWIG
{% 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.

11 years ago

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 %}

TXT


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

Ah I see yup that worked LOL

Awesome all done :) :D

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 :D

11 years ago

Me again :( 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.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1328 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago