More Related Content Similar to MT meets PHP - PHP conference Kansai 2013 Similar to MT meets PHP - PHP conference Kansai 2013 (20) MT meets PHP - PHP conference Kansai 20132. About Me (野⽥田 純⽣生)
•アルファサード株式会社 代表取締役
•CMS/受託開発を通じてクライアントを爆速化
•Movable Type エバンジェリスト
•http://junnama.alfasado.net/online/
•http://twitter.com/junnama
• https://www.facebook.com/junnama.noda
25. Example 1
<?php
$blog_id = 2;
include('/path/to/mt/php/mt.php');
$mt = MT::get_instance($blog_id,
'/path/to/mt/mt-config.cgi');
$blog = $mt->db()->fetch_blog($blog_id);
echo $blog->name . "n";
?>
26. Example 2
<?php
require_once( 'class.mt_entry.php' );
$_entry = new Entry;
$where = "entry_status=2 and
entry_blog_id=2";
$where .= " order by entry_authored_on
DESC ";
$extra['limit'] = 10;
$entries = $_entry->Find( $where, FALSE,
FALSE, $extra );
foreach ( $entries as $entry ) {
echo $entry->title . "n";
}
?>
27. Example 3
<?php //class.mt_log.php
require_once( "class.baseobject.php" );
class Log extends BaseObject {
public $_table = 'mt_log';
protected $_prefix = "log_";
}
?>
<?php
require_once( 'class.mt_log.php' );
$_log = new Log;
$_log->message = “This is test.”;
$ts = gmdate( "YmdHis" );
$_log->created_on = $ts;
$_log->modified_on = $ts;
$_log->Save();
?>
29. MTML=Movable Type
Markup Language
•MTML=Viewを担当するもの
•MTIf MTElse MTLoopなど、実はロジックも書け
る
•テンプレートにプログラムは書けない(完全に分
離離している)=>デザイナに敷居が低い
•<MTFoo> でも <mt:foo> でも <mtfoo /> で
も <$MTFoo$> でも良良い(緩い)
37. ブロックタグ(2)
! } else {
! ! $out = $ctx->stash('__out');
! ! $counter = $ctx->__stash['vars']
['__counter__'] + 1;
! ! if ( $glue && $content && $out ) {
! ! ! $content = $glue . $content;
! ! } else {
! ! ! $ctx->stash('__out', TRUE);
! ! }
! ! if ( $counter <= $to ) {
! ! ! $ctx->__stash['vars']['__counter__']
= $counter;
! ! ! $repeat = TRUE;
! ! } else {
! ! ! $repeat = FALSE;
! ! }
! ! return $content;
! }
38. ブロックタグ(3)
<mt:SampleBlock from="1" to="10" glue=",">
</mt:SampleBlock>
! 1,2,3,4,5,6,7,8,9,10
42. drwxrwxrwx 32 alfasado admin 1088 3 10 15:17 .
drwxr-xr-x 11 alfasado admin 374 3 8 13:40 ..
-rw-rw-rw- 1 alfasado admin 935 2 27 16:08 .htaccess
-rw-rw-rw- 1 alfasado admin 1223 2 27 16:08 .mtview.php
-rw-rw-rw- 1 alfasado admin 24609 3 2 14:41 index.html
50. Example 7
$terms = array(‘status’ => 2,
‘blog_id’ => 2,
‘class’=>‘entry’);
$args = array(‘sort’ => ‘authored_on’,
‘direcrtion’ => ‘descend’,
‘limit’ => 10 );
$entries = $app->load(‘Entry’,$terms,$args);
SELECT * FROM ‘mt_entry’
WHERE entry_status=2 AND
entry_class=‘entry’
ORDER BY `mt_entry`.`entry_authored_on` DESC
LIMIT 0,10
51. Example 8
class ExamplePlugin extends MTPlugin {
'config_settings' => array( // mt-config.cgi
'DynamicForceCompile' => array( 'default' => 0 ),),
'settings' => array( // PluginSettings
'example_setting' => array( 'default' => 1 ),),
'tags' => array( // Template Tags
'block' => array('dynamicmtml' => 'dynamicmtml'),
'function' => array('authorlanguage' => 'authorlanguage'),
'modifier' => array('trimwhitespace' => 'trimwhitespace'),),
'task_workers' => array( // Workers
'mt_rebuild' => array('label' => 'Publishes content.',
'code' => 'workers_mt_rebuild',
'class' => 'MT::Worker::Publish',),),
'callbacks' => array( // Callbacks
'build_page' => 'filter_build_page',),);
mt_dir/plugins/EsamplePlugin/php/config.php
57. <!-- File: /app/controllers/posts_controller.php -->
<?php
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts',
$this->Post->find('all'));
}
function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
}
?>
Original(Controller)
58. <!-- File: /app/views/posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th><th>Title</th><th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo
$html->link($post['Post']['title'],
array('controller' => 'posts',
'action' => 'view',
$post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?
></td>
</tr>
<?php endforeach; ?>
</table>
Original(View)
59. <!-- File: /app/controllers/posts_controller.php -->
<?php
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$ctx = $this->ctx;
$ctx->__stash['vars']['page_title']
= 'Blog posts';
$ctx->stash('Post',
$this->Post->find('all'));
}
function view( $id = null ) {
$ctx = $this->ctx;
$this->Post->id = $id;
$ctx->stash('Post', $this->Post->read());
}
}
?>
DynamicMTML(Controller)
60. <!-- File: /app/views/posts/index.ctp -->
<h1><mt:var name="page_title" escape="html"></h1>
<mt:cake:loop model="Post">
<mt:if name="__first__">
<table>
<tr>
<th>Id</th><th>Title</th><th>Created</th>
</tr>
</mt:if>
<tr>
<td><mt:var name="id"></td>
<td>
<a href=".view/<mt:var name="id">">
<mt:var name="title" escape="html"></a>
</td>
<td><mt:var name="created"></td>
</tr>
<mt:if name="__last__">
</table>
</mt:if>
</mt:cake:loop>
DynamicMTML(View)