Age calculation in php
Declare a function =>
- function getAge( $dob, $date )
- {
- $dob=strtotime($dob);
- $date=strtotime($date);
- $age = 0;
- while( $date > $dob = strtotime('+1 year', $dob))
- {
- ++$age;
- }
- return $age." years";
- }
$this variable.getSkinUrl($path) or getUrl($path), are contained in all block classes. These methods are then used in a template file, by calling: $this->getSkinurl('images/logo.png'), for example.toHtml() method for this purpose. Usually, the toHtml() method will simply parse the template file and output it to the user, but we could override that functionality and return anything we like, such as XML or JSON.app/
code/
{core,local,community}/
Company/
Module/
Block/
design/
frontend/
base/
default/
layout/
template/
mytheme/
default/
layout/
template/
christmas/
layout/
template/
skin/
frontend/
base/
default/
css/
images/
mytheme/
default/
css/
images/
christmas/
css/
images/
.phtml files found in various folders under theapp/design/frontend/{$interface}/{$theme}/template folder. All layout XML files will be found in theapp/design/frontend/{$interface}/{$theme}/layout folder. All blocks will be in theapp/code/{core,local,community}/{$company}/{$module}/Block folder.Mage_Customer module is requesting that customer.xml be loaded to define the layout for the customer/account/login page we’re trying to display. Let’s also assume that we’ve already created an interface called “newtheme”; have three themes called “christmas,” “5 for 1,” and “default”; and have specified that we’d like to use the “christmas” theme in the Magento administration area.customer.xml:app/design/frontend/newtheme/default/template/catalog/product/view.phtml into theapp/design/frontend/newtheme/christmas/template directory. But if we decided that the CSS would stay the same, then we’d leave the styles.css file in skin/frontend/newtheme/default/css where it is, and it would still be loaded.<layout>
<customer_account_register>
<block name="wrapper">
<block type="core/template" template="page/html/header.phtml">
<block name="navigation" template="page/html/navigation.phtml" />
</block>
</block>
</customer_account_register>
</layout>
/customer/account/register page on the website, which is referenced in the XML as customer_account_register, called the layout handle.{$module}/{$controller}/{$action}. This gives an enormous clue as to which layout handle in the XML file to edit: {$module}_{$controller}_{$action}./>) or can contain other blocks.type and a name.toHTML() method to generate its output.header.phtml file mentioned in the layout XML:<html>
<head>
<title><?php echo $this->getTitle() ?></title>
</head>
<body>
<h1><a href="<?php echo $this->getUrl() ?>"><img src="<?php echo $this->getSkinUrl('images/logo.png') ?>" /></a></h1>
<?php echo $this->getChildHtml('navigation') ?>
<p>Welcome to my shop!</p>
$this variable that was previously mentioned. There are some standard methods that are made available to all blocks, such as:getUrl($path): using the Base URL defined in the Magento administration area, this method will generate a full URL, which is useful if you’re moving from a development environment to production, for example. Example usage: $this->getUrl('customer/account/login') will producehttp://www.example.com/customer/account/login.getSkinUrl($path): this is the same idea as the getUrl() method, but works with the skin directory found in “skin/frontend” and the interface and theme active in the Magento Administration. The same hierarchical lookup is done on files in the skin directory as is done with template files.getChildHtml($name): this allows us to call nested or child blocks defined in the layout. We’ll be looking at this in more detail shortly.getTitle() function in our example layout file is only available to the core/template block—which makes sense, as its role is to output the page’s title element.getChildHtml() method. In the layout XML above, you can see that the header block has another block nested inside it. Magento won’t know where to output the child’s HTML in the template file, so we need to explicitly define where to put it by calling thegetChildHtml($name) method, and providing the name of the child block we want to display as the first argument. Child blocks are a great way to separate out parts of a page into reusable components.defaultSTORE_{$theme}THEME_frontend_{$interface}_{$theme}{$module}_{$controller}_{$action}<default> handle.default handle.<reference> tag solves this problem.<layout>
<default>
<block name="wrapper">
<block type="core/template" template="page/html/header.phtml">
<block name="navigation" template="page/html/navigation.phtml" />
</block>
<block type="core/template" template="page/html/footer.phtml" />
</block>
</default>
<customer_account_register>
<reference name="wrapper">
<block type="core/template" name="customer_hello" template="customer/helloworld.phtml" />
</reference>
</customer_account_register>
</layout>
<layout>
<default>
<block name="wrapper">
<block type="core/template" template="page/html/header.phtml">
<block name="navigation" template="page/html/navigation.phtml" />
</block>
<block type="core/template" template="page/html/footer.phtml" />
</block>
</default>
<customer_account_register>
<reference name="wrapper">
<block type="core/template" name="customer_hello" template="customer/helloworld.phtml" />
<remove name="footer" />
</reference>
</customer_account_register>
</layout>
header and footer blocks are actually wrapped by a block called wrapper, of type text/list. To explain how this works, we need to head back to the toHtml() method of a block.toHtml() method of the text/list block will inspect all its child blocks, and then, individually, call thetoHtml() methods on those child blocks. The order of the blocks being output is, generally, the order in which they appear in the XML; however, we can override this behavior with the after and before attributes. So the wrapper is just a list of other blocks that will be displayed in a certain order.<layout>
<default>
<block name="wrapper">
<block type="core/template" template="page/html/header.phtml" >
<block name="navigation" template="page/html/navigation.phtml" />
</block>
<block type="core/template" template="page/html/footer.phtml"/>
</block>
</default>
<customer_account_register>
<reference name="wrapper">
<block type="core/template" name="customer_hello" template="customer/helloworld.phtml" after="header" />
</reference>
</customer_account_register>
</layout>
customer.xml in your theme’s layout folder. Instead, apply your changes to a different file calledlocal.xml.local.xml file lives in your app/design/frontend/{$interface}/{$theme}/layout directory, and will likely contain numerous update handles by the end of the theming process.local.xml file automatically, so any changes you make will be pulled into the site.mycompany with your desired interface name):app/design/frontend/
mycompany/
default/
layout/
local.xml
template/
skin/frontend/
mycompany/
default/
images/
css/
local.xml file can remain empty for the moment.base/default. This is normal: Magento is following the include hierarchy, having noticed that the files in question don’t exist in our theme yet. base/default doesn’t have a styles.css file, so all the styles have disappeared. To give ourselves a starting point, let’s take the style sheets from skin/frontend/default/default/css and put them into ourskin/frontend/mycompany/default/css folder. Likewise, move the images from default/default to theimages folder in your new theme. As you modify the theme, you can delete any of these that you won’t be using, but for now it’s good to have it to look at while you work.default layout handle.app/design/frontend/base/default/layout/ to find the name of the block with the type and template we’re looking for. If your editor or IDE has a folder-wide search option, this is a great time to put it to use!Mage_Core_Block_Template and the templatecallouts/right_col.phtml. The PayPal logo has the block type Mage_Paypal_Block_Logo and the templatepaypal/partner/logo.phtml.paypal.xml:<block name="paypal.partner.right.logo" template="paypal/partner/logo.phtml"/>
catalog.xml:<block name="right.permanent.callout" template="callouts/right_col.phtml">
<action method="setImgSrc"><src>images/media/col_right_callout.jpg</src></action>
<action method="setImgAlt" translate="alt" module="catalog"><alt>Keep your eyes open for our special Back to School items and save A LOT!</alt></action>
</block>
action tags; what’s important is that we now have our names:paypal.partner.right.callout and right.permanent.callout.<?xml version="1.0"?>
<layout>
<default>
<remove name="paypal.partner.right.logo" />
<remove name="right.permanent.callout" />
</default>
</layout>
before andafter attributes to achieve our desired layout.<reference name="right">, because the blocks appear in the right column; we’re also looking for the block type and template names we can see in the red boxes on the front end.<default>
<reference name="right">
<block type="poll/activePoll">
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
</reference>
</default>
action tags; all they do is allow you to call a method from the block class from within the layout file. We can see that there are currently no before or after attributes defined for the block. Our local.xml file now can be updated to:<?xml version="1.0"?>
<layout>
<default>
<remove name="paypal.partner.right.callout" />
<remove name="right.permanent.callout" />
<reference name="right">
<remove name="right.poll" />
<block type="poll/activePoll" before="cart_sidebar">
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
</reference>
</default>
</layout>
app/design/frontend/mycompany/default/template; we’ll create a new folder called sponsor, and a file inside it called superbig.phtml. That file can contain whatever HTML you want to display as the advertisement.core/template, as we’re not going to require any functionality that warrants the creation of a new block:<?xml version="1.0"?>
<layout>
<default>
<remove name="paypal.partner.right.callout" />
<remove name="right.permanent.callout" />
<reference name="right">
<remove name="right.poll" />
<block name="right.poll" before="cart_sidebar">
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
<block type="core/template" template="sponsor/superbig.phtml" />
</reference>
</default>
</layout>
base/default style sheet that’s being loaded at the moment. Let’s include another CSS file, so that we can add our own tweaks.head block in the layout file.head Blockhead block as it stands. It’s located in page.xml:<block name="head" as="head">
<action method="addJs"><script>prototype/prototype.js</script></action>
<action method="addJs" ifconfig="dev/js/deprecation"><script>prototype/deprecation.js</script></action>
<action method="addJs"><script>lib/ccard.js</script></action>
<action method="addJs"><script>prototype/validation.js</script></action>
<action method="addJs"><script>scriptaculous/builder.js</script></action>
<action method="addJs"><script>scriptaculous/effects.js</script></action>
<action method="addJs"><script>scriptaculous/dragdrop.js</script></action>
<action method="addJs"><script>scriptaculous/controls.js</script></action>
<action method="addJs"><script>scriptaculous/slider.js</script></action>
<action method="addJs"><script>varien/js.js</script></action>
<action method="addJs"><script>varien/form.js</script></action>
<action method="addJs"><script>varien/menu.js</script></action>
<action method="addJs"><script>mage/translate.js</script></action>
<action method="addJs"><script>mage/cookies.js</script></action>
<action method="addCss"><stylesheet>css/styles.css</stylesheet></action>
<action method="addItem"><type>skin_css</type><name>css/styles-ie.css</name><params/><if>lt IE 8</if></action>
<action method="addCss"><stylesheet>css/widgets.css</stylesheet></action>
<action method="addCss"><stylesheet>css/print.css</stylesheet><params>media="print"</params></action>
<action method="addItem"><type>js</type><name>lib/ds-sleight.js</name><params/><if>lt IE 7</if></action>
<action method="addItem"><type>skin_js</type><name>js/ie6.js</name><params/><if>lt IE 7</if></action>
</block>
action tags allow us to call the methods associated with the block classes.addJs: this allows us to include JavaScript that’s located in the /js directory at the root of our Magento installation.addItem: this allows us to include assets that we have in our skin directory for our theme.<action method="addItem"><type>$type</type><name>$name</name><params>$params</params><if>$if</if></action>
$type that you’ll most likely use in a career as a Magento designer/developer are:skin_jsskin_css$name refers to the location of your asset from the skin/frontend/mycompany/default/ folder, so a style sheet in skin/frontend/mycompany/default/css/updates.css will have the $name css/updates.css.$params value can be used to pass parameters to the <link /> tag, such as the media the style sheet is applicable to.$if value allows you to wrap a style sheet in conditional comments for Internet Explorer.head Blockhead block and how we can use it to include various assets; let’s add our own style sheet.default layout handle. We’re updating an already defined block, so we’ll be using a reference.<?xml version="1.0"?>
<layout>
<default>
<remove name="paypal.partner.right.callout" />
<remove name="right.permanent.callout" />
<reference name="right">
<remove name="right.poll" />
<block type="poll/activePoll" before="cart_sidebar">
<action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
<action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
</block>
</reference>
<reference name="head">
<action method="addItem">
<type>skin_css</type>
<name>css/updates.css</name>
<params />
<if />
</action>
</reference>
</default>
</layout>