开发者

Custom meta tag configuration

I have 2 tables, page and settings.

page is just a bunch of fields, such as name and slug, and has 3 other fields for meta tags (title, keywords, description) and displays a cms page.

The settings has 3 fields: default_meta_title, default_meta_keywords, default_meta_description

Now what I'm looking to do is to display the default_meta_* tags in the HTML source if the page I am on does not have the particular meta info set from the cms page.

All pages, except the homepage is managed this way, so I was thinking I'd need to add some code to the layout.php to get this to work.

So the homepage will display my default_meta_*, a开发者_高级运维s I cannot set this in the cms pages table.


There are two ways to solve the problem.

First is to use sfYaml class to update view.yml with default meta tags (see documentation about view.yml). After that if specific page should use another metas you can override defaults with addMeta method of response object

Second (as ManseUK suggested) is to declare slot placing code like this into layout

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <?php include_javascripts() ?>
    <?php include_stylesheets() ?>
    <?php include_title() ?>
    <?php if (has_slot('metas')): ?>
      <?php include_slot('metas') ?>
    <?php else: ?>
      <?php include_component('page', 'metas') ?>
    <?php endif; ?>

    <link rel="shortcut icon" href="/favicon.ico" />
  </head>
  <body>

Default metas will be rendered via page components. On top of your template (i guess modules/page/templates/showSuccess.php) place code

<?php slot('metas') ?>
  <?php if($page->hasMetas()):?>
    <!-- code to render nondefault page metas -->
    <?php echo $page->getMetas(); ?>
  <?php else: ?>
    <?php include_component('page', 'metas') ?>
  <?php endif;?>
<?php end_slot() ?>

I assume that you will replace $page->hasMetas() with real code that will check if your page object has metatags.

Actually i would prefer to go further and code page components to accept parameters. Code in a template will look like

<?php slot('metas') ?>
  <?php include_component('page', 'metas', array('metas'=>$page->getMetas())) ?>
<?php end_slot() ?>

Deciding which metas (default or not) should be rendered will take place in page components (i assume that you can easily retrieve defaul;t settings from your database). If no parameters were passed (see layout code) than your component should also render default metas.

I hope this will help.


You could use a slot - check for existence of the slot in the layout - if it exists then add the custom meta fields - if not add the default ones

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜