framework questions. How can I reuse the same template? - php

I'm hoping I can get a definite answer on this. I will try and explain this as best I can.
I have a main template file that contains everything I want. In a small area within this template, I want to echo the view that is associated with the link that the user clicks on. This is not a problem so far however, when I have to use a loop data it is not so easy anymore.
If I have this as my template:
table
tr
td><?php echo $myContent; ?></td
/tr
/table
You can clearly see how I would be able to echo the values of the variable. Now, suppose that I wanted to show the user something that required multiple rows of data. I would then have to use a loop. So, you see, it depends on the content that I want to display that determines what construct to use within the HTML.
Can someone please help me to figure this out? I'm sure this is just me not correctly understanding how this should be done.
Thanks

Have a HTML files with custom tags of your own.
say template.html:
<div><$content$></div>
when processing the output, you take the contents of template.html and parse it:
$replace = array();
$replace['<$content$>'] = $pagecontents;
$template = file_get_contents('template.html');
$output = str_replace(array_keys($replace),$replace,$template);
Adapted from http://code.google.com/p/samstyle-php-framework/.

Related

PHP | How would I create a "plugin system" to inject content/HTML onto my page?

I am trying to create a function that can inject HTML in between certain div tags using PHP.
For example lets say I have the function with the following parameters. add_html('<div class = "greeting">', 'hello')
How would I make it so that the text hello was added inside of <div class = "greeting"> like so: <div class = "greeting">hello</div> My goal is to use PHP for this. How would I go about implementing this? What would be the most simple way to accomplish this?
A function could be like so:
<?php
function add_div($class, $content) {
return sprintf('<div class="%s">%s</div>', $class, $content);
}
echo add_div('foo', 'Hello Earth!');
Output:
<div class="foo">Hello Earth!</div>
But it doesn't really save you much typing.
You've changed the particulars of it, but the fundamental issue remains unanswerable with any kind of specifics that it seems you're looking for. I am struggling to even express how many variables there are in what you're asking. I'm putting this as an answer, not because it answers your question, but because comments are insufficient to address it.
Your current example is that you want a "greeting" div. Okay. But where do you want it? On every page? On the home page? At the top or bottom or somewhere in between? Let's assume that your CMS has a way of knowing what overall layout is going to be used (headers, footers, menus, sidebars, etc.), and what specific template is going to be used to generate the page-specific content (text and pictures of a blog post, for example), and that you want to give your plugins a way to inject their own content into the layout right at the top of the content (above the blog text and pictures). This is now a nice, specific sort of question that StackOverflow is better at answering, instead of such a general open-ended one that you have.
Okay, let's address that particular question. Ideally, you would show your CMS code for generating a page. Let's pretend it's something like this:
echo $this->layout->headers();
echo $this->layout->menu();
echo $this->template->content();
echo $this->layout->footers();
It's probably actually more complex than that, but this will do for an example. Now, we can say that if you want to use a plugin hook architecture, you would add something like this after the menu call:
foreach ($this->plugins as $plugin) {
if (method_exists($plugin, 'inject_pre_content')) {
$plugin->inject_pre_content();
}
}
and you would add an inject_pre_content function in your plugin which echoes your desired HTML.
If you wanted to use event hooks instead, then in the same place you might instead simply add:
$this->triggerEvent('inject_pre_content');
but this relies on you also incorporating an event handling framework.
Plugin hooks are, IMO, easier to build, but not as powerful.
Hope that this helps you to understand what you're up against a little bit better, and clarifies what the two main options might look like.

how to input a chunk of html with php

I am currently working on a website that uses a login system I made.
I have a div that holds a bar that goes on the top of the website. This bar is supposed to change depending if you're a guest, logged in, or a admin. (Therefor there are three separate divs - guestmode, membermode and adminmode)
I have tried to make php print the html when wanted, and it worked. The only problem is that it's messy and not at all easy to edit.
Is it possible for php to print html without using 'echo' or 'print', but copying from another file?
Basically I want to copy normal html syntaxed-code from a file somewhere and turn it into something that php can print, then of course print it.
Is there something out there that can do that?
Thanks in advance!
For PHP, use something like:
echo file_get_contents('path/to/my_html_snippet.my_ext'); // echo instantly
$stored = file_get_contents('path/to/my_html_snippet.my_ext'); // or save it for later in a string
Create a file at path/to/my_html_snippet.my_ext that contains the HTML you want to print.
Its seems like you need a templates.
See mustache (examples and docs - https://github.com/bobthecow/mustache.php)

WordPress 3.1.3 and Magic Fields PlugIn - calling and parsing multiline or markdown textbox into single.php?

Since a couple of days I am working on some WordPress Page for a friend of mine who is owning a company for time work. They are working with some software, that is, at the end, producing a HTML file with some tables, spans and stuff. This file actually is a template, they use to search for new employees. To make sure, they don't have the work of setting up the employment ad twice (working with 2 different web sites), I offered them, to set up a field to enter the HTML sourcecode, containing CSS, Tables, Spans and a lot of "dirty" stuff. Since tables are not
very search enginge optimized (don't let's talk about W3C), I told them to use divs instead. - what a mistake I made :)
Anyway: I used some lines of code like:
<?php
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://www.prophiler.de/rentaman2/stellenanzeige');
foreach($html->find('SPAN.SHeadcompany') as $a)
{
$a->class = null;
$a->innertext;
echo $a->innertext = '<div class="head1">' . $a->innertext . '</div>';
}
... continue with all SPAN classes...
?>
to parse the file and write the contents back into div classes.
The problem I have is to call AND parse the custom field actually. This is why I called the HTML file through an invisible WordPress Page. This was for presentation only. Now I would like to call the field value directly and append it to the parser. I don't get it, since I am pretty new to PHP.
Is anybody out there, able to give me some hints on how to handle it?

Newbie Question about adding an array element on a page (php script)

Apologize me if the question is unclear.
I'm a total newbie php user and I have this script that I want to edit.
The script have uses .tpl for the theme.
In the memberprofile.tpl there is the first name element $profilearray[0].firstname I want to add the result of this element also in another .tpl file called docs.tpl
I tried copying and pasting `$profilearray[0].firstname in docs.tpl but it did not work.
I noticed that docs.tpl uses its own $docsarray[0].xxx
So guys any idea how to do this? because there are some information in the member profile that I would like to add them inside the docs page.
I tried playing with mysql but I don't know how to use the same element firstname for both memberprofile and docs tables
I'm sure there is an easy way to do it.
Here's the full code from memberprofile.tpland I want to show some of these info in that theme docs.tpl
<p class="gray">
{$lang112}: <b>{$profilearray[0].firstname} {$profilearray[0].lastname}</b><br>
{$lang130}: <b>{$profilearray[0].birthday}</b><br>
{$lang134}: <b>{if $profilearray[0].gender eq "1"}Male{elseif $profilearray[0].gender eq "0"}Female{/if}</b><br>
{$lang140}: <b>{$profilearray[0].city}</b> <br>
{$lang139}: <b>{$profilearray[0].country}</b> <br>
{$lang113}: <b>{insert name=get_stripped_phrase value=a assign=pdesc details=$profilearray[0].description}{$pdesc}</b> <br>
{$lang259}: <b>{$profilearray[0].url|stripslashes|truncate:20:"...":true}</b> <br>
{$lang260}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].lastlogin}</b> <br>
{$lang261}: <b>{insert name=get_time_to_days_ago value=var time=$profilearray[0].addtime}</b>
</p>
Smarty has a tag you can google for called {include} that will do what you want. http://www.smarty.net/docsv2/en/language.function.include.tpl Use that tag on your docs.tpl file and you will be fine.
$profilearray
in your example is assigned to smarty template before the template is called in some fashion simmilar to this
$smarty->assign('profilearray',$somearray);
you'll need to find what is setting that in your first file then make sure that is included in your second template call
but you should certainly read the smarty documentation to understand what you are trying to do.
To step back for a moment... There are two parts to all this. The first part is the PHP code which actually takes user input, queries the database, processes the data, etc. The second part is the TPL file. As much as possible, the TPL file should only be concerned with presentation, not with data-processing, data-cross-referencing, etc.
All that database-reading and cross-referencing should happen inside a plain PHP file, not inside the TPL.
In order to add "Author information" to the "Documents List" (or whatever you call docs.tpl) page, you need to find the PHP code which pulls up the list of documents. Find the PHP code where it says something like:
$smarty->assign('docsarray',$document_list);
Now what you want to do is pass more information to the smarty template (TPL file) so that it can display it. Something like:
for($document_list as $index => $doc){
$owner = $doc['owner']; // Get the owner of the document
$profile = getProfile($owner); // Create one of the same things that go into $profilearray elsewhere
$document_list[$index]['profile'] = $profile; // Modify original array
}
$smarty->assign('docsarray',$document_list);
Then go into docs.tpl and find where it displays information about each document, and add smarty template code to read from the new per-document information you added. (Check the Smarty reference page for details.)
For example, if docs.tpl shows a table of documents, you might add a new column that shows the author's first/last name:
<tr>
<td>{$docsarray[$index].title}</td>
<td>{$docsarray[$index].created_date}</td>
<!-- Next line is new -->
<td>{$docsarray[$index].profile.firstname} {$docsarray[$index].profile.lastname}</td>
</tr>
If you want something that looks exactly like the "profile box", you can do that too. In fact, using {include} you can create profilebox.tpl and use it in both places to reduce redundant code.

Get ID of a DIV element using php

-- edit --
I've made the question far more detailed here: Drupal aggregators and tailored output
-- original question --
Is it possible to get the id of an html div element on any given page using php so I can do something like:
if($divid == 'id-of-div') { do this; }
--edit--
To clarify:
I have a page on a website. It could be any page. On that website I have a div element which I want to be able to target using php to populate depending on the id of that div element. For example:
if($divid == 'div1') { $output = 'div 1 output'; }
if($divid == 'div2') { $output = 'div 2 output'; }
return $output;
Hope that helps...
-- re-edit(!) --
I've commented below but think it would probably be more useful in here:
Hi, sorry I'm not being more clear. I'm not a php expert (as you can probably tell!). I am theming a Drupal site at the moment and have a function that generates output to a block. These are the same kinds of block but they are unique by virtue of their div id's. I want to be able to tailor the generated output to these depending on which div area it is. Does that make any more sense? Thanks for your help so far, by the way
I think you are misunderstanding the way PHP works. PHP is usually used to generate the raw HTML code of a page, but you usually don't use it to traverse the DOM of the output like you can in jQuery. (In theory, you can do this using output buffering and a HTML parser class, but it makes no sense at all.)
There shouldn't be the need to find elements by ID because in order to put something into a DIV, you can do the following:
<div id="mydiv">
<?php echo "This is inside mydiv"; ?>
</div>
The resulting HTML will be a DIV that contains whatever you told PHP to output. This is usually enough.
Not sure whether this helps you. If it doesn't, maybe add some more detail about what you want to do.
For this use hidden varriables

Categories