Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
Hello! I just wonder how I can separate Header from index.php

I have made header.php and writed like this
Code: Select all
<?php require_once("couch/cms.php") ?>

   <header class="logo-main">
   <img src="<cms:editable
   name="logoimg"
   label="Image"
   desc="Upload your Logo here."
   width="100%"
   height="300"
   type="image"/>
      <section class="logo-text"> Text here</section>
   </header>

<?php COUCH::invoke(); ?>


And I want this to come to my index.php, i tried <cms:show "header.php" /> but the Logo don't come, and I tried embed code too.
Hi,

The first point to keep in mind would be that the <?php require_once("couch/cms.php") ?> and <?php COUCH::invoke(); ?> should be placed only in the templates i.e. the PHP files that are listed in Couch admin-panel's sidebar and can be accessed directly through browser using their names e.g. http://www.yoursite.com/index.php or http://www.yoursite.com/blog.php (where index.php and blog.php would be the templates).

The templates themselves can include other files - in Couch we know these included files as 'snippets'. These are supposed to be placed in the 'snippets' folder and are included using the <cms:embed /> tag. These snippets are not meant to be accessed directly through browser. We use them as separate files only to reuse their code in multiple templates.

With the distinction understood, I think it should be clear that in your use-case the 'index.php' is the template and the 'header.php' is the snippet (I think you can easily appreciate that you wouldn't want header.php to be accessed directly e.g. http://www.yoursite.com/header.php and hence it is not a template but a snippet).

So now if the original code in index.php was -
Code: Select all
<?php require_once("couch/cms.php") ?>

<header class="logo-main">
    <img src="<cms:editable
        name="logoimg"
        label="Image"
        desc="Upload your Logo here."
        width="100%"
        height="300"
        type="image" />"
    />
    <section class="logo-text"> Text here</section>
</header>

<?php COUCH::invoke(); ?>

- we can put the header code in a snippet named 'header.html' -
Code: Select all
    <img src="<cms:editable 
        name="logoimg"
        label="Image"
        desc="Upload your Logo here."
        width="100%"
        height="300"
        type="image" />"
    />
    <section class="logo-text"> Text here</section>

- and embed the snippet in index.php as follows -
Code: Select all
<?php require_once("couch/cms.php") ?>

<cms:embed 'header.html' />

<?php COUCH::invoke(); ?>


and it would show up exactly as if the code was placed directly in index.php.

Now I think it'd be safe to assume that you'd want the same header to appear in other templates also e.g. in blog.php or contact.php.

If you were to place the header.html snippet in any other template, you'll run into a little side-effect that could be undesirable.

Take a look at header.html and you'll see that you are defining an editable region ('logoimg') within it.

So, if this snippet is used in, say, blog.php, the editable region would get defined in that template too (remember, cms:embed is exactly equivalent to placing the snippet code right there within the containing template).

This, I think, is not what you are looking for. You probably want to define an image editable region only once and use it everywhere on the site (i.e. in all templates where we embed the snippet).

To do that, please see the 'Global values' section in the following page our our docs - http://www.couchcms.com/docs/tutorials/ ... -ends.html

Assuming you followed the method outlined in the said document and created the editable region (named 'logoimg') in a global template named 'globals.php', the header.html snippet can now be modified to become this -
Code: Select all
    <img src="<cms:get_custom_field 'logoimg' masterpage='globals.php' />" />
    <section class="logo-text"> Text here</section>

Now the logo image will appear in all templates where you embed header.html.

Hope this helps.
Did come back from work so answer litle bit late. But Thanks for the answer. Works great!
3 posts Page 1 of 1