Jump to content

Image syntax in php template?


antknight
 Share

Recommended Posts

if logo is an image field, then by default it is an array of images. In the field settings (details), you can change the max number of images from 0 (unlimited) to 1, and this will make your 'logo' field into a single image instead of an array. For the array, you'd have to grab the first image by:

$logo = $page->logo->first();

or you can get a resized version with:

$logothumb = $page->logo->first()->width(100);

or

$logothumb = $page->logo->first()->height(100);

-> 100 being the pixel size

Then, to output that logo, you can do something like:

echo "<img src='{$logothumb->url}' />";

If you set your image field to max 1 image, then you can skip the first() step and just use:

$logothumb = $page->logo->width(100);

and maybe even just do it all at once with:

echo "<img src='{$page->logo->url}' />";

or

<img src="<?php echo $page->logo->url ?>" />

see more info here:

http://processwire.com/api/fieldtypes/images/

  • Like 1
Link to comment
Share on other sites

I'm assuming that logo is an image field. Edit your 'logo' image field settings. On the details tab, what is the max number of images set at? For something like a logo, I"m going to assume you've set that at 1, since you typically wouldn't have more than 1 logo. If that is the case, the code to output it would be:

<img src='<?=$page->logo->url?>' alt='Logo' />

If your max number of images is set at 0 (no max) or something more than 1, then you'd have to determine which image you want to pluck from the logo field. Lets say you want to grab the first image out of it:

<img src='<?=$page->logo->first()->url?> alt='Logo' />

Or lets say you want to show them all:

<?php foreach($page->logo as $file) echo "<img src='$file->url' alt='' />"; ?>
  • Like 1
Link to comment
Share on other sites

Cynical answer, yes in the API docs http://processwire.com/api/fieldtypes/ there's all the infos you need. But people don't seem to find it or read it. And in the forums there's tons of. Search google with "site:processwire.com keywords".

But I know what you mean, and I also think there's so many snippets hiding in the forums it would be nice to have a wiki like repository or something. But then we started it long time ago http://wiki.processwire.com , just nobody seems to take the time to build it. Also some lonely is trying to make something here http://clintonskakun.com/processwire-docs/

Link to comment
Share on other sites

I think the main problem is that I dont know any PHP beyond the odd include, so the syntax is what I have trouble with. I do try to find snippets in the forum but sometimes I don't know what keywords to use. I think you are right though about using google.

Link to comment
Share on other sites

well, don't worry if your php is minimal, with PW you can get pretty far with just knowing how to:

1) set variables:

$variable = whatever;

2) echo:

echo "any text, but usually html like <div> stuff in my div </div>";

echo $page->body;

3) using foreach:

foreach ($pagearray as $oneofthepages) {

echo "<div>{$oneofthepages->body}</div>";

}

just those with the api will do great things for you, my friend :)

  • Like 1
Link to comment
Share on other sites

Ant,

Stick with it is all I can say. Neil's right, you'll get by with essentially

Getting the page/fields you want through the API

$blog_entries = $pages->find("template=blog_entry");

and looping through the content

foreach ($blog_entries as $entry) {

echo "<h2>$blog->title</h2>;

}

Essentially all you need to do most of the time and what PW does so efficiently is: access your data, then display it how you want in your templates.

It will become second nature within not too long at all.

The good news is that though you only need the basics to do almost any site, the possibilities are endless as you can achieve pretty much anything with PHP (which you'll learn from some of the genius' on here!)

Link to comment
Share on other sites

About the repository: I think a good way to do this would be to have some kind of social bookmarking here in the forum. This would make it pretty easy for people, when they have some free time, to go a bit back in the forums and start collecting the useful snippets and share it with everyone. This would avoid all the copy/pasting to the wiki, and make the collecting work a lot simpler. If IP.Board doesn't offer this kind of functionality, maybe we can prepare a simple page on the PW site and build a bookmarklet for this.

Link to comment
Share on other sites

What about using gist and embedding those on the forum? Gist embeds are simply: <script src="https://gist.github.com/4146884.js?file=gistfile1.txt"></script> (of course not yet supported, needs plugin for this board/editor).

That would fix indentation problems, but also would offer version history, support for multiple files and it would be very easy to build central repo to just referencing to those gists.

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