Jump to content

Is it possible to examine "Fields" on content?


Ralf
 Share

Recommended Posts

Hi all,

somewhat since my last visit here in the forum but now I have something time and many pages that I would like to realize with ProcessWire ago.

Let's restart with a simple question probably for you.

In the past I used the CMS Conpresso and there in the template, it was possible to examine "Fields" on content.

For example:

<div class="content">
	<div><!-- DROPDOWN: Event; --><!-- ifNotSet: Event; --><!-- Text: Event_alternative; --><!-- /ifNotSet --></div>
	<div><!-- TEXT: Datum; format=D j.m.; --></div>
	<!-- ifSet: Homepage; --><div><a href="http://<!-- URL: Homepage; -->" target="_new"><!-- Text: Homepage; --></div><!-- /ifSet -->
	<!-- ifSet: Departure_AUI; --><div>Aui: <!-- TEXT: Departure_AUI; --></div><!-- /ifSet -->
	<!-- ifSet: Departure_MAG; --><div>Mag: <!-- TEXT: Departure_MAG; --></div><!-- /ifSet -->
</div>

My problem now is that I'm not sure how do I convert this in HTML/PHP?

- Has ProcessWire its own solution for this task setting?

- Can I solve the problem with an "if-clause"?

- someting else...

Sorry for this confusion but could someone help me on the jumps with this problem? I know, the handling of the frontends in ProcessWire is free but I'm not soo familiar with php and actual I have no own "style".

Many thanks

Ralf

Link to comment
Share on other sites

Hi Ralf,

If I understand correctly, it should be a simple matter of:

if($page->Departure_AUI){
    echo "<div>Aui: {$page->Departure_AUI}</div>";
}

There are shorter/cleaner ways of doing this, but I think this is the most understandable for someone new to php.

Hope that helps!

  • Like 2
Link to comment
Share on other sites

@ adrian

thanks for your fast answer!! And you´re right you understood me correctly and i´m new at php respectively i can read a little bit  :-[

I will try the code asap and if i have further questions, i´ll come back...

Purely out of interest, how does a "better / faster" code look like? i would happy to learn something new.

@ pwired

also like to thank. When i have the situation with pictures i´ll come back to your code.

Link to comment
Share on other sites

 Purely out of interest, how does a "better / faster" code look like? i would happy to learn something new.

alternatives: http://processwire.com/talk/topic/5640-shortest-way-to-echo-a-field-only-if-populated/

But if I were new to PHP I would write a few chars more and have better readability.

EDIT: damn, Kongondo!

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

I tend to agree with horst on this, but it can make life a lot easier if you learn some basic implementations of ternary operators, depending on how you want to structure your html and php.

This can be easier:

echo $page->Departure_AUI ? "<div>Aui: {$page->Departure_AUI}</div>" : "";

In my mind it really comes down to whether that div can be left empty, or if it must not be echo'd at all, which depends on your layout.

It can also be easier sometimes to build up sections of content in a variable first.

$out = '';
$out .= $page->Departure_AUI ? "<div>Aui:{$page->Departure_AUI}</div>" : "";
$out .= $page->Departure_MAG ? "<div>Mag: {$page->Departure_MAG}</div>" : "";
echo "<div id="block-parent">$out</div>";
  • Like 3
Link to comment
Share on other sites

echo "<div id="block-parent">$out</div>";

Thanks adrian,

but i changed your last line with the following code that it works like a charm   ;)

echo "<div id=\"block-parent\">" . $out . "</div>";
  • Like 1
Link to comment
Share on other sites

Thanks for fixing my sloppy code :)

Also, here is a great article on ternary operators:

http://davidwalsh.name/php-shorthand-if-else-ternary-operators

Anything you think you might be sacrificing in PW by not using templating syntax, is more than made up by the abilities of simple everyday PHP. Ryan also wrote a good article here: http://processwire.com/api/why-php-syntax/

  • Like 2
Link to comment
Share on other sites

@ adrian

THANK you very much!!

This two articles are great... and with this sentence

Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if/else structures.

and your code example

echo $page->Departure_AUI ? "<div>Aui: {$page->Departure_AUI}</div>" : "";
made it "click" in my head :biggrin:

and again I learned something important, now I have to do only the "cheatsheet" and finally understand it myself, this would certainly dissolve another big knot in my head ... but one step after the other :-[

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