xoops_view プラグイン

xoops_view プラグインは、強力ですがあまりお行儀がよろしくない Smarty プラグインです。
このプラグインと、プリロードファイル、カスタムテンプレートを組み合わせることで、他のモジュールの好きなデータをとってきて任意のテンプレートの任意の場所に表示することができます。

試してみる

(0)Smarty プラグインディレクトリ(html/class/smarty/plugins または xoops_trust_path/libs/smarty/plugins …XCL2.2の場合)に、以下のコードを function.xoops_view.php という名前で保存する

(1)表示したいテンプレートに <{xoops_view template=user_custom.html delegate=Module.user.GetCustomData options=1}> を記述する。

(2)以下のコードを html/user/preload/ に CustomDataPreload.class.php という名前で保存する。

(3)Altsys > テンプレート で、カスタムテンプレートを追加する。名前は (1)の template= で指定した名前(user_custom.html)。テンプレートの中身は、例えば

<{$data->get('uname')}>

※uid=1 の人の uname を表示

このサンプルではユーザ名を表示しているだけで、あまり有難味はありませんが、デリゲートで取得するデータと、追加したカスタムテンプレートの中身を変えることで、好きなモジュールのデータを好きに表示することができます。
デザイナの方にとってはデリゲートのコードはちょっと難易度が高いので、プログラマの人にお願いして、カスタムテンプレートを頑張るという分担もできるかと思います。

(0) function.xoops_view.php

<?php
function smarty_function_xoops_view($params, &$smarty)
{
	$delegate = $params['delegate'];
	$template = $params['template'];
	$dirname = $params['dirname'];
	$options = explode('|', $params['options']);

	//check required parameter
	if(! $delegate) return;

	//get data
	$result = null;
	XCube_DelegateUtils::call($delegate, new XCube_Ref($result), $dirname, $options);
	if($result===false){
		return;
	}
	
	//render template
	if(isset($template)){
		$render = new XCube_RenderTarget();
		$render->setTemplateName($template);
		$render->setAttribute('legacy_buffertype',XCUBE_RENDER_TARGET_TYPE_MAIN);
		$render->setAttribute('data', $result);
		XCube_Root::getSingleton()->getRenderSystem('Legacy_RenderSystem')->render($render);

		echo $render->getResult();
	}
	else{
		echo $result;
	}
}
?>

(2) CustomDataPreload.class.php

<?php
if(!defined('XOOPS_ROOT_PATH')){
	exit;
}

class User_CustomDataPreload extends XCube_ActionFilter
{
	public function preBlockFilter()
	{
		$this->mRoot->mDelegateManager->add('Module.user.GetCustomData','User_CustomDataPreload::show');
	}

	public static function show(/*** mixed ***/ $result,/*** string ***/ $dirname, /*** mixed ***/ $options)
	{
		$id = intval($options[0]);
		$handler = xoops_gethandler('user');
		if($id>0){
			$result = $handler->get($id);
		}
	}
}
?>