{"id":712,"date":"2004-09-13T16:09:48","date_gmt":"2004-09-13T16:09:48","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2004\/09\/alternating_table_rows_with_template_toolkit_and_css-shtml\/"},"modified":"2004-09-13T16:09:48","modified_gmt":"2004-09-13T16:09:48","slug":"alternating_table_rows_with_template_toolkit_and_css-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/","title":{"rendered":"Alternating Table Rows With Template Toolkit And CSS"},"content":{"rendered":"<p>\nWhilst looking at a potential recode of the <a href=\"http:\/\/www.smashhitschart.com\/\">Smash Hits Chart<\/a> website, I came across the need to make each alternate row in the main chart table a different colour.\n<\/p>\n<p>\nAs the recode will be using <a href=\"http:\/\/www.template-toolkit.org\/\">Template Toolkit<\/a>, this is simple.\n<\/p>\n<p>\nThe chart is passed as a list, so we have to iterate over it to get each entry out. I do this using a simple <code>&#091;&#037; FOREACH &#037;&#093;<\/code>. Template Toolkit thoughtfully provides us with a <a href=\"http:\/\/template-toolkit.org\/docs\/default\/Modules\/Template\/Iterator.html\">Template::Iterator<\/a> object called <var>loop<\/var> inside the <code>FOREACH<\/code> block, so we can use the <a href=\"http:\/\/template-toolkit.org\/docs\/default\/Modules\/Template\/Iterator.html#index_\"><code>index()<\/code><\/a> method to get the current elements list position. Using this, we just need to check if the value is odd or even to decide what colour to make the row.\n<\/p>\n<p>\nLet&#8217;s see some sample code. To change the row&#8217;s colour I&#8217;m going to use two <acronym title=\"Cascading Style Sheet\">CSS<\/acronym> classes called <var>lite<\/var> and <var>dark<\/var>.\n<\/p>\n<div class=\"code\"><code>&lt;table&gt;<br \/>\n&#091;&#037; FOREACH entry = chart &#037;&#093;<br \/>\n&lt;tr class=\"&#091;&#037; IF loop.index % 2 &#037;&#093;lite&#091;&#037; ELSE &#037;&#093;dark&#091;&#037; END &#037;&#093;\"&gt;<br \/>\n&lt;td&gt;&#091;&#037; entry.position &#037;&#093;&lt;\/td&gt;<br \/>\n&lt;td&gt;&#091;&#037; entry.artist &#037;&#093;&lt;\/td&gt;<br \/>\n&lt;td&gt;&#091;&#037; entry.track &#037;&#093;&lt;\/td&gt;<br \/>\n&lt;tr&gt;<br \/>\n&#091;&#037; END &#037;&#093;<br \/>\n&lt;\/table&gt;<br \/>\n<\/code><\/div>\n<p>\nThe magic is in the line <code>loop.index % 2<\/code>. It takes the remainer (modulus) of <var>loop.index<\/var> divided by 2. This returns either 0 or 1, which is what Template Toolkit uses to determine truth, so can be used directly in the <code>IF<\/code> statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whilst looking at a potential recode of the Smash Hits Chart website, I came across the need to make each alternate row in the main chart table a different colour. As the recode will be using Template Toolkit, this is simple. The chart is passed as a list, so we have to iterate over it &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Alternating Table Rows With Template Toolkit And CSS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[47],"class_list":["post-712","post","type-post","status-publish","format-standard","hentry","category-dev","tag-perl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Alternating Table Rows With Template Toolkit And CSS - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Alternating Table Rows With Template Toolkit And CSS - Robert Price\" \/>\n<meta property=\"og:description\" content=\"Whilst looking at a potential recode of the Smash Hits Chart website, I came across the need to make each alternate row in the main chart table a different colour. As the recode will be using Template Toolkit, this is simple. The chart is passed as a list, so we have to iterate over it &hellip; Continue reading &quot;Alternating Table Rows With Template Toolkit And CSS&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2004-09-13T16:09:48+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Alternating Table Rows With Template Toolkit And CSS\",\"datePublished\":\"2004-09-13T16:09:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/\"},\"wordCount\":185,\"keywords\":[\"Perl\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/\",\"name\":\"Alternating Table Rows With Template Toolkit And CSS - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2004-09-13T16:09:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/alternating_table_rows_with_template_toolkit_and_css-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Alternating Table Rows With Template Toolkit And CSS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Alternating Table Rows With Template Toolkit And CSS - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Alternating Table Rows With Template Toolkit And CSS - Robert Price","og_description":"Whilst looking at a potential recode of the Smash Hits Chart website, I came across the need to make each alternate row in the main chart table a different colour. As the recode will be using Template Toolkit, this is simple. The chart is passed as a list, so we have to iterate over it &hellip; Continue reading \"Alternating Table Rows With Template Toolkit And CSS\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/","og_site_name":"Robert Price","article_published_time":"2004-09-13T16:09:48+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Alternating Table Rows With Template Toolkit And CSS","datePublished":"2004-09-13T16:09:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/"},"wordCount":185,"keywords":["Perl"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/","name":"Alternating Table Rows With Template Toolkit And CSS - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2004-09-13T16:09:48+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/alternating_table_rows_with_template_toolkit_and_css-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Alternating Table Rows With Template Toolkit And CSS"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}