Last active
December 12, 2015 00:39
-
-
Save yurivictor/4685615 to your computer and use it in GitHub Desktop.
Template function
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 | |
/** | |
* Load a template. MVC FTW! | |
* | |
* @param string $template the template to load, without extension (assumes .php). File should be in templates/ folder | |
* @param args array of args to be run through extract and passed to template | |
*/ | |
function template( $template, $args = array() ) { | |
extract( $args ); | |
if ( ! $template ) | |
return false; | |
$path = dirname( __FILE__ ) . "/templates/{$template}.php"; | |
$path = apply_filters( 'my_key', $path, $template ); | |
include $path; | |
} | |
/** EXAMPLE USAGE **********************************************************/ | |
/** | |
* Gets json feed and converts to usable posts | |
* | |
* @uses template | |
* @return html | |
*/ | |
function get_feed( $feed, $template ) { | |
$data = file_get_contents( $feed ); | |
$json = json_decode( $data ); | |
$posts = $json->posts; | |
template( $template, compact( 'posts' ) ); | |
} | |
?> | |
<?php /** EXAMPLE HTML ***********************************************************/ ?> | |
<?php foreach ( $posts as $post ): ?> | |
<li class="borB1S padBT10"> | |
<a href="<?php echo $post->url; ?>"> | |
<?php if ( $post->attachments[0]->url ): ?> | |
<img src="<?php echo get_template_directory_uri()?>/inc/imrs.php?src=<?php echo $post->attachments[0]->url; ?>&h=130&w=220" height="130" swidth="220" /> | |
<?php endif; ?> | |
<h5><?php echo $post->title; ?></h5> | |
</a> | |
</li> | |
<?php endforeach; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment