Jump to content

Learning to use the API


bwakad
 Share

Recommended Posts

It's not totally wrong what you're saying, but their is more then meets the eye.

<li>Result: <code><?php echo $page->child;?></code> and <code2><?php echo $page->child->id;?></code2></li>

<li>Both will give you the ID of the <em>first child</em> page, starting from the current viewed page.</li>
Ryan was so nice to use the 'magic methode' __toString() for the Page object, so excessing the object it self will out put a string with the page id  | and output the integer id
Edited by Martijn Geerts
Link to comment
Share on other sites

I see you are talking about the core code. Well, I just was testing with '->id', and although I can get the ID of the page I'm after (exept children = more then 1),

it does not do anything else then that. Combining for instance '->id->name', or 'title' is out of the question. Why would we anyway? We can get it directly without the ->id !

Right now I work on my second sheet which are names.

Yesterday, as I started with php. I thought let me see each vs foreach. Just for fun. The result is quite different.

So the big question for me is: how are things indexed on a page level. I mean, on a empty page I guess you would have: ID | Name | Label | Title - as standard things. So directly accesible as in $page->title;

Fields (custom) and their values are probably only retrievable by for example selector or get vs find.

Link to comment
Share on other sites

Not sure if you already got this, but just to be clear: you don't just "receive" one or more results, you explicitly ask for either of those. A get() will return one result that you can use directly, even if the selector you use would return more, and a find() will return an array that you have to itterate through, even if the result of that select is only one page.

It's not totally wrong what you're saying, but their is more then meets the eye

I see you are talking about the core code. Well, I just was testing with '->id', and although I can get the ID of the page I'm after (exept children = more then 1)

I think, more than anything, Diogo and Martijn are highlighting to you the importance of semantics and syntax within your code: though the syntax produces a result, even one you might be able to work it, it's semantically confusing, implicit, and error prone. I know you're just testing the code, please don't think anyone is trying to badger you.  Test, play, code all you need to in order to grasp the API.  I think everyone just wants to help you get these small concepts and keep them in mind because they are important.

  • Like 2
Link to comment
Share on other sites

I never used each() in my live  :P  

I do, all the time in jQuery  ;)  But yeah, traversing an array manually in PHP with each()  is a random occurrence for me too. Used in conjunction with list(), it's slow and fickle. foreach() FTW

  • Like 1
Link to comment
Share on other sites

Can one explain to me: semantics and syntax within your code?

As for the code I posted above, I deleted the one with ->id (except for template) since it does not do anything extra.

The way I think any page can be reached is like this. It's just an example based on a current page. In reality, any page could have a template, any page could be current, which would end up in different child/parent...:

<h5>Example</h5>
<ul>
    <li>parent page | template</li>
    <ul>
        <li>rootParent page</li>
        <ul>
            <li>siblings page</li>
            <li>$page (current) | template</li>
            <ul>
                <li>child page</li>
                <li>child page</li>
            </ul>
        </ul>
    </ul>
</ul>
Link to comment
Share on other sites

Can someone explain this for me: $page->fields. API states: All the Fields assigned to this page (via it's template, same as $page->template->fields). Returns a FieldsArray. But I only receive back 1.

At first it will give me the template which is used. When using foreach I will get the fields used. So the values received back are quite different.

Link to comment
Share on other sites

It's not garbled code - view the page source and it will be better formatted showing you all the fields in the template on the page. Echoing $page->fields shows the template name because of the way things are configured, but I honestly don't think there is a real need to ever do that, because you get the same thing from $page->template->name which is more logical if that's what you are after.

While it's great to learn and understand the inner workings of the code, I sometimes feel like you are trying to learn too much too soon. Spend more time building and using what you need, rather than dissecting everything. You'll figure out the minor details when the need arises, rather than confusing you before you a full handle on the basics. Does that make sense?

For a basic PW site you really just need to know that you can echo out any field with $page->fieldname and that an images field either returns a single image, or an array of images depending on the maximum files allowed setting. If you have an array you can foreach through it. That's really the extent of things you need to build your first site. Using your current question - how often do you really need to get all the fields on a given page? 

I am really not trying to criticize your approach, I just think you'll get more for your effort by just building stuff. Either way, we'll continue to help, but it's always more fun helping with solutions to practical questions :)

  • Like 4
Link to comment
Share on other sites

I understand what you say adrian (and others), and I really appreciate all answers. But here's the thing:

I started to build - but did not know: everybody say 'play with the code'.

I started to play - and try to learn: everybody say 'build'.

Now, the only reason I ask Pete to change the topic title is so it's clear to everyone - I am learning - instead of having my name popup all over the forum categories (because my topic should go in an appropriate place).

Meanwhile, since the start of this topic it was not clear to me why for instance some kinda function would output the name/value, but with other fields it did not.

So by learning about fields I found out why:

$allfields = $page->fields; foreach ($allfields as $myfield) echo $myfield;

Output: title, body, branche, provincie

All the Fields assigned to this page (via it's template).

Using echo $myfield->title I thought 'why do not all echo out? I only get back from fields 'branche' and 'provincie'. So what kinda types are there?

'Going one step further I used:

$allfields = $page->fields; foreach ($allfields as $myfield) echo $myfield->type;

->type which by the way I could not find on the cheatsheet!

Output: FieldtypePageTitle, FieldtypeTextarea, FieldtypePage, FieldtypePage

Looking at those fieldtypes it also made sence to me:

My $field already represents $page->title, $page->body, $page->branche, and $page->provincie. It has no meaning to do something like: $myfield->title which would be translated as $page->title->title!

So, yes - I can build, but I keep learning, looking at the logic before taking your time with questions I should know the answer of. I don't want to know all, but need to know more before I'm building. And mind you, it's more fun learning this way because I get to practice PW and PHP at the same time!

Link to comment
Share on other sites

I understand what you say adrian (and others), and I really appreciate all answers. But here's the thing:

I started to build - but did not know: everybody say 'play with the code'.

The idea here is get the basics right; not try and get to know everything, especially the deeper stuff in one swoop! :-) Rome wasn't built in a day; but it was built "correctly" from the start; The builders got the basics right :-)

I started to play - and try to learn: everybody say 'build'.

Yes, but play "in your league" (no offence meant here). You are trying to play in the big league but you are still a rookie; going by your questions, we can tell you are not ready yet. Rookies need time to learn. So, play in the minor leagues first, then advance upward....I promise you, it will be more enjoyable for you this way...

In essence, yes build, but build slowly, build the right way with simple tools first (forget PHP functions and the rest, for instance) - concentrate on the PW API but not the whole cheatsheet! - just $page, $pages and selectors for starters. As you build this way, also play but play according to your current knowledge (level)...

I think I have said enough....based on my own experience. Final decision is up to you, of course. I am out....

  • Like 4
Link to comment
Share on other sites

Starting to like the way PW make things happening! Dynamic all the way...

When I am on a page the happens to be the same name as my field for page selects I can simply do:

$filter = $pages->find("template=products,  limit=50, sort={$page->name}");
foreach ($filter as $child) {

// and I can even output my custom field value without even writing the field name:

$field = $page->name ; echo $child->$field->title ;
Link to comment
Share on other sites

  • 2 weeks later...

You can give the choice of having a sidebar or moving it from left to right with something as simple as this:

1. create two checkbox fields "remove_sidebar" and "sidebar_right" with the labels "remove the sidebar" and "move sidebar to the right" and add them to your template.

2. add this to the template file:

<?php if(!$page->remove_sidebar): ?>
    <div class="sidebar<?php if($page->sidebar_right) echo 'float_right'; ?>">
        <!-- html code for the sidebar -->
    </div>
<?php endif; ?>

This will print the code for the sidebar only if the first checkbox is not checked (notice the !), and will add the "float_right" class to it only if the second checkbox is checked.

Then you just have to float .float_right to the right in your stylesheet. This is only an example of class and css, of course.

  • Like 3
Link to comment
Share on other sites

diogo,

You interpreted that question a lot differently than I did. :P


*nice example though*

I do pretty much exactly that for a few sites. I've actually been thinking about creating a layout picker field type that allows those options to be set via icons. I used to help with a WP install that allowed you to choose layouts like that. Always seemed very intuitive.

  • Like 1
Link to comment
Share on other sites

I hope I interpreted it correctly :)

Besides giving a working example, what I meant was to answer that you do have to use the API, but that it is extremely simple to do and it will also be very simple for the editors.

  • Like 1
Link to comment
Share on other sites

Well, after one month I must say, I did learn more of php and PW then any other software. So glad I suddenly see this software mentioned some time ago.

I started from the DEV branch. Made my fields and pages.

Made 1 template which I use to browse main and sub categories.

Made 1 template to browse a single item.

I have 2 files - 1 for search-form and 1 for results which I eventually will delete since I want to use it on my browse template.

Added foundation css. Did my layout. My pagination is ready on all pages. And everything is working as expected.

Next for me is a:

adding tooltip markup for google markers (don't know how yet);

registration form for front-end submission;

front end submission;

and registered user page (profile page).

Does someone have suggestions for this???

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