Jump to content

Using a variable from one template on another


BFD Calendar
 Share

Recommended Posts

I have an 'events_births' template that has a date field and then calls a name and place from a 'people' and 'places' page.

Both 'name' and 'place' require some calculation from their respective pages, some people have original names or aliases, some places have a name and street address, while others haven't. They are calculated in '$namecalc' and '$placecalc'.

Now I want to use the '$namecalc' and '$placecalc' show up in a list of events. I manage to list the date field, but find no way for the other information.

Secondly I'd like the '$placecalc' items show up on a map....

My code on the 'events_births' template:

// NAME FROM PEOPLE PAGE

    $namepage = $pages->get("template=bfd_people, id=$page->bfd_events_people_id_list");
    $occupation = ucfirst($namepage->bfd_people_occupation->title);
    if($namepage->bfd_people_original) {
	$originally = ", originally " . $namepage->bfd_people_original;
	};
	if($namepage->bfd_people_alias) {
	$alias = " aka " . $namepage->bfd_people_alias;
	};	
	$namecalc = "<b><a href='{$namepage->url}'>{$occupation} {$namepage->bfd_people_name_first} {$namepage->bfd_people_name_middle} {$namepage->bfd_people_name_last}{$originally}{$alias} </a></b> is born ";
	echo $namecalc;
                          		
// LOCATION FROM PLACES PAGE
		   
    $placepage = $pages->get("template=bfd_places, id=$page->bfd_events_places_id_list");
    if($placepage->bfd_places_name) {
	$name = $placepage->bfd_places_name . ", ";
	};
	if($placepage->bfd_places_street) {
	$street = $placepage->bfd_places_street . ", ";
	};
	if($placepage->bfd_places_district) {
	$district = $placepage->bfd_places_district . ", ";
	};
	if($placepage->bfd_places_state) {
	$state = $placepage->bfd_places_state->title . ", ";
	};
	$placecalc = "in <b><a href='{$placepage->url}'>{$name}{$street}{$district}{$placepage->bfd_places_city->title}, {$placepage->bfd_places_state->title}{$placepage->bfd_places_country->title}</a></b>.";
    echo $placecalc;

On the list page:

    echo "<span class='nobelbold_32px'>" . date("j") . " " . date ("F") . "</span><br><br>";
    $todayday = date("d");
    $todaymonth = date("m");
    $features = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, sort=bfd_year");
    foreach($features as $feature)
    $name = $feature->get("$nameresult");
    echo "<li><span class='nobelbold_24px_grey'><a href='{$feature->url}'>{$feature->bfd_year}</a> {$feature->bfd_case->title} {$name}</span></li>";
    
    $mapitems = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, MapMarker=' ', sort=bfd_year");
	$map = $modules->get('MarkupGoogleMap'); 
	echo "<br>{$map->render($mapitems, 'MapMarker', array('height' => '400px', 'zoom' => '2', 'lat' => '50.923813', 'lng' => '4.431493'))}"; 

Needless to say that my PHP knowledge is limited compared to most here.... thanks anyway.

Link to comment
Share on other sites

You can save variables to the page array easily (without having to create input fields first).

$page->setOutputFormatting(false);
$page->food = array('apple','banana','orange','grape','pasta','sandwich','bread','coffee');
$page->save();
$page->setOutputFormatting(true);

echo "<b>Shopping List:</b><br>";
foreach($page->food as $f) {
	echo "$f <br>";
}

You should be able to call such a var from any other tpl / page then, as if it was a tpl-field: $foo = $pages->get(345)->varname. Maybe that way you can store these calculated values for further use in the list?

Link to comment
Share on other sites

You can save variables to the page array easily (without having to create input fields first).

That works fine. This is the variable on 'bfd_events_births':

    $namepage = $pages->get("template=bfd_people, id=$page->bfd_events_people_id_list");
    $occupation = ucfirst($namepage->bfd_people_occupation->title);
    if($namepage->bfd_people_original) {
	$originally = ", originally " . $namepage->bfd_people_original;
	};
	if($namepage->bfd_people_alias) {
	$alias = " aka " . $namepage->bfd_people_alias;
	};
	$page->namecalc = array('<b><a href=', $namepage->url, '>', $occupation, ' ', $namepage->bfd_people_name_first, ' ', $namepage->bfd_people_name_last, $originally, $alias, '</a></b> is born ');
	foreach($page->namecalc as $f) {
	echo "$f";
	};

You should be able to call such a var from any other tpl / page then, as if it was a tpl-field: $foo = $pages->get(345)->varname. Maybe that way you can store these calculated values for further use in the list?

This is more complicated. The variable should show up in a list on the home page. That list gets information from any child page/template from parent '/events/'.

So, what should I understand from '345'? And can I assume that 'namecalc' is the varname in my example?

And in general where can one find/learn more about this, preferably in plain language?

Link to comment
Share on other sites

Still wrestling with this....

    $features = $pages->find("parent=/events/, bfd_day.name=$todayday, bfd_month.name=$todaymonth, sort=bfd_year");
    
    foreach($features as $feature) {
    
	$namepage = $feature->bfd_events_people_id_list;
	
	if($namepage->bfd_people_occupation->title) {
    $occupation = ucfirst($namepage->bfd_people_occupation->title);
    };
   	if($namepage->bfd_people_original) {
	$originally = ", originally " . $namepage->bfd_people_original;
	};
	if($namepage->bfd_people_alias) {
	$alias = " aka " . $namepage->bfd_people_alias;
	};	
	echo "{$occupation}<b><a href='{$namepage->url}'> {$namepage->bfd_people_name}</a>{$originally}{$alias}</b>";

results in:

  • occupation1 name1 originally1 (= ok because there is no alias)
  • occupation2 name2 originally1 alias2 (= not ok because it repeats originally1 while there is no originally)
  • occupation2 name3 originally1 alias2 (= not ok because it repeats originally1 while there is no originally and it repeats alias2 while there is no alias)

What am I doing wrong?

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