Last active
August 29, 2015 14:11
-
-
Save outflux3/fb7e7dc3ed6c711842f9 to your computer and use it in GitHub Desktop.
code snippets for modern web design class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// featured | |
$slides = $pages->find("template=slide, featured=1"); | |
// Word Limiter | |
function wordLimiter(HookEvent $event){ | |
$field = $event->arguments[0]; // first argument | |
$limit = $event->arguments[1]; | |
$endstr = isset($event->arguments[2]) ? $event->arguments[2] : ' …'; | |
$page = $event->object; // the page | |
$str = $page->get($field); | |
$str = strip_tags($str); | |
if(strlen($str) <= $limit) return; | |
$out = substr($str, 0, $limit); | |
$pos = strrpos($out, " "); | |
if ($pos>0) { | |
$out = substr($out, 0, $pos); | |
} | |
return $event->return = $out .= $endstr; | |
} | |
// this will add a custom method to Page object | |
wire()->addHook("Page::wordLimiter", null, "wordLimiter"); | |
// List Assets | |
function listAssets($asset_files) { | |
$templates = wire('config')->urls->templates; | |
foreach ($asset_files as $asset_file) { | |
$path_parts = pathinfo($asset_file); | |
if($path_parts['extension'] == 'js' ) { | |
echo "<script type=\"text/javascript\" src=\"{$templates}{$asset_file}\"></script>\n\t\t"; | |
} elseif($path_parts['extension'] == 'css' ) { | |
echo "<link rel=\"stylesheet\" href=\"{$templates}{$asset_file}\">\n\t\t"; | |
} | |
} | |
} | |
// Create Mailto | |
function createMailto($strEmail){ | |
$strNewAddress = ''; | |
for($intCounter = 0; $intCounter < strlen($strEmail); $intCounter++){ | |
$strNewAddress .= "&#" . ord(substr($strEmail,$intCounter,1)) . ";"; | |
} | |
$arrEmail = explode("@", $strNewAddress); | |
$strTag = "<script language="."Javascript"." type="."text/javascript".">\n"; | |
$strTag .= "<!--\n"; | |
$strTag .= "document.write('<a href=\"mai');\n"; | |
$strTag .= "document.write('lto');\n"; | |
$strTag .= "document.write(':" . $arrEmail[0] . "');\n"; | |
$strTag .= "document.write('@');\n"; | |
$strTag .= "document.write('" . $arrEmail[1] . "\">');\n"; | |
$strTag .= "document.write('" . $arrEmail[0] . "');\n"; | |
$strTag .= "document.write('@');\n"; | |
$strTag .= "document.write('" . $arrEmail[1] . "<\/a>');\n"; | |
$strTag .= "// -->\n"; | |
$strTag .= "</script><noscript>" . $arrEmail[0] . " at \n"; | |
$strTag .= str_replace("."," dot ",$arrEmail[1]) . "</noscript>"; | |
return $strTag; | |
} | |
// Use in Init | |
$minifyCSS = true; | |
$minifyJS = true; | |
if($user->hasRole('superuser')) { | |
$minifyCSS = false; | |
$minifyJS = false; | |
} | |
$stylesheets = array( | |
'assets/css/bootstrap.min.css', | |
); | |
$jsfiles = array( | |
'assets/js/jquery-1.11.2.min.js' | |
); | |
// Use in Head | |
if($minifyCSS == true) { | |
// if using procache | |
echo $procache->link($stylesheets); | |
// if using AIOM+ | |
echo '<link rel="stylesheet" type="text/css" href="' . AllInOneMinify::CSS($stylesheets) . '" />'; | |
} else { | |
listAssets($stylesheets); | |
} | |
// Use in Foot | |
if($minifyJS == true) { | |
// if using procache | |
echo $procache->script($jsfiles); | |
// if using AIOM+ | |
echo '<script type="text/javascript" src="' . AIOM::JS($jsfiles) . '"></script>'; | |
} else { | |
listAssets($jsfiles); | |
} | |
// or if not using Procache, use the alternative AIOM+ code. | |
################################################################# | |
# Browser Titles | |
################################################################# | |
$title = $page->title; | |
/******** SECTION TITLE **********/ | |
$pageSection = ''; | |
if( $page->parent->is('template=news-index|album-index|artist-index') ) $pageSection = ' | ' . $page->parent->title; | |
$browserTitle = $title . $pageSection . ' | ' . $site_title; | |
/******** HOMEPAGE TITLE **********/ | |
if ( $page == $homepage && !isset($homepage_title) ) $browserTitle = $title . ' | ' . $site_title; | |
if ( $page == $homepage && !isset($homepage_title) && $title == $site_title ) $browserTitle = $site_title; | |
if ( $page == $homepage && isset($homepage_title) ) $browserTitle = $homepage_title; | |
// this is in the header: | |
<!-- OPEN GRAPH | |
================================================== --> | |
<meta property="og:title" content="<?=$browserTitle?>" /> | |
<meta property="og:type" content="website" /> | |
<meta property="og:url" content="<?=$page->httpUrl?>" /> | |
<meta property="og:site_name" content="<?=$site_title?>" /> | |
<meta property="og:locale" content="en_us" /> | |
<?php if($meta_description) { ?> | |
<meta property="og:description" content="<?=$meta_description?>" /> | |
<?php } ?> | |
<?php if(isset($ogImage)) { ?> | |
<meta property="og:image" content="<?=$ogImage->httpUrl?>"/> | |
<?php } ?> | |
<?php if(isset($fb_admins)) { ?> | |
<meta property="fb:admins" content="<?=$fb_admins?>" /> | |
<?php } ?> | |
<?php | |
// this is in the init | |
/* Metas | |
------------------------------------------------------------- */ | |
$meta_description = ''; | |
if($page->summary !== '') $meta_description = $page->summary; | |
if(!$meta_description && $page->body !=='') { | |
$meta_description = $page->wordLimiter("body", 165); | |
$meta_description = str_replace(array("\r", "\n"), '', $meta_description); | |
} | |
/* Schema | |
------------------------------------------------------------- */ | |
//http://schema.org/Florist | |
$schema = array( | |
"@context" => "http://schema.org", | |
"@type" => "Florist", | |
"url" => "http://www.mygreatsite.com", | |
"name" => "My Great Site", | |
"telephone" => $contact_phone, | |
"legalName" => "My Great Site, LLC" | |
); | |
$schema['logo'] = $logo_dark->httpUrl; | |
foreach($socials as $social) { | |
$schema['sameAs'][] = $social->value; | |
} | |
?> | |
<!-- this gets placed in the head, to output the json/ld --> | |
<script type="application/ld+json"> | |
<?php echo json_encode($schema); ?> | |
</script> | |
/* Image for Facebook | |
-------------------------------------*/ | |
if($post->images->count()) { | |
if($post->images->findTag("featured")->count()) { | |
$ogImage = $post->images->findTag("featured")->first()->width(850); | |
} else { | |
$ogImage = $post->images->first()->width(850); | |
} | |
} | |
<?php if( isset($ogImage) && isset($twitter_handle) ) { ?> | |
================================================== --> | |
<meta name="twitter:card" content="summary_large_image"/> | |
<meta name="twitter:description" content="<?=$meta_description?>"/> | |
<meta name="twitter:title" content="<?=$browserTitle?>"/> | |
<meta name="twitter:site" content="<?=$twitter_handle?>"/> | |
<meta name="twitter:image" content="<?=$ogImage->httpUrl?>"> | |
<?php } ?> | |
<?php | |
// this is a meta description, using the summary field: | |
<meta name="description" content="<?=$page->summary?>"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment