Jump to content

function that outputs header


Recommended Posts

I have created a function that holds/outputs links such as logout, sign in... etc.Here is it:

function output_header($username=false) {?><div id="header">		 <ul id="headelem">    <li><?php echo $username ?></li>	 <li><a href="contact.php">Επικοινωνία</a></li>	 <li><a href="day.php">Calendar</a></li>    <li><a href="login.php">Είσοδος</a></li>    <li><a href="register_form.php">Εγγραφή</a></li>    <li><a href="logout.php">Αποσύνδεση</a></li>	 </ul></div><?php}

When called, the above li items will appear in the browser. My aim though, is that I want the logout link appearing ONLY after the user has signed(that is the logical thing to do, anyway) A scheme I have though is sth like that:

function output_header($username=false,$logout=false) {?><div id="header">		 <ul id="headelem">    <li><?php echo $username ?></li>	 <li><a href="contact.php">Επικοινωνία</a></li>	 <li><a href="day.php">Calendar</a></li>    <li><a href="login.php">Είσοδος</a></li>    <li><a href="register_form.php">Εγγραφή</a></li>    echo $logout	 </ul></div><?php}

Where logout will be replaced by the li item, which will be a string as an argument of the function. What do you think about the above? Do you have to propose anything better?

Link to post
Share on other sites

I would not use a function just to print something. It will make it harder to maintain if you mix codes with visual representation and logics.also it makes it little messy when you look. You can just make a template file of header for authenticated user and inject them (include()) if user are logged in. you can also make another template for unregister user and inject them same way when user is not logged in.

Edited by birbal
Link to post
Share on other sites

Is it easy to write a code example so I can grasp better this "template logic"? Thanks in advance.

Link to post
Share on other sites

there is several template engines. popular is smarty. if you see this i am sure you will get the idea of templates. though i don't like template engines as php itself is template engine and in some points it feels like they are creating another language based on php which adds a processing layer too.

  • Like 1
Link to post
Share on other sites

So Smarty is a template engine, not a framework?

  • Like 1
Link to post
Share on other sites

yes smarty is template engine.

Link to post
Share on other sites

template engine is a system which uses templates (in this case html template) to generate pages. templates makes an outline of the document and the changing data is inserted into placeholders. Framework is some sort of library build around some well worked design patterns maintaining some sort of relation on each components to get done certain jobs efficiently. common development works is moved to framework to enhance speed of development process. eg an template engine could be a part of framework. (an MVC framework may use template engines for Views)

Edited by birbal
  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...