Mahara日本語ドキュメント/基本PHPファイルテンプレート: Difference between revisions
From Mahara Wiki
< Mahara日本語ドキュメント
No edit summary |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
以下、すべての新しいPHPファイル用のテンプレートです。あなたのファイルに外部からアクセスできるか否かにより、異なるテンプレートがあります。 | 以下、すべての新しいPHPファイル用のテンプレートです。あなたのファイルに外部からアクセスできるか否かにより、異なるテンプレートがあります。 | ||
Line 45: | Line 43: | ||
'''メモ:''' | '''メモ:''' | ||
* | * php閉じタグ "?>" は入れないでください。 | ||
* | * 勤務時間にプロジェクトの作業を実施した場合、Catalyst社員は著作権表示 "Catalyst IT Ltd" を追加してください。 | ||
* 作者 (author) フィールドには、会社ではなく、常に人 (1名または複数名) を指定してください。 | |||
== pieformsおよびdwooを使用したサンプルページ == | |||
<source lang="php" enclose="div"> | |||
<?php | |||
define('INTERNAL', 1); | |||
require('init.php'); | |||
require_once('pieforms/pieform.php'); | |||
// Get data from the user by using the param_ functions, which will whitelist | |||
// the input | |||
$userint = param_integer('int',0); | |||
// If none of the whitelisting options is acceptable, you can use param_variable, | |||
// but be careful not to print the results to the screen without cleaning them | |||
// first. That's a XSS vulnerability. | |||
$userraw = param_variable('raw', 'no user input provided'); | |||
// Generate page content | |||
// NOTE: Normally you should put hard-coded strings into a lang file under /lang, so that | |||
// they can be internationalized with get_string() | |||
$hardcoded = "This is a hard-coded string"; | |||
// See https://wiki.mahara.org/index.php/Developer_Area/Core_Subsystems/Form_API_%28Pieforms%29 | |||
// Normally you'd populate the default values with values from the DB | |||
$form = array( | |||
'name' => 'testform', | |||
'method' => 'post', | |||
'action' => "", // self | |||
'successcallback' => 'testform_submit', // defaults to name_submit | |||
'validatecallback' => 'testform_validate', // defaults to name_validate | |||
'elements' => array( | |||
'fullname' => array( | |||
'type' => 'fieldset', | |||
'legend' => "Your name", // should use get_string()... | |||
'elements' => array( | |||
'firstname' => array( | |||
'type' => 'text', | |||
'title' => "First name", // should use get_string() | |||
'description' => "Your first, or given name", // get_string() | |||
'help' => "Surely you know your own first name.", // get_string() | |||
'defaultvalue' => 'Joe', | |||
'rules' => array( | |||
'required' => true | |||
) | |||
), | |||
'lastname' => array( | |||
'type' => 'text', | |||
'title' => "Last name", // get_string() | |||
'defaultvalue' => 'Schmoe', | |||
'description' => "Your last, or family name", // get_string() | |||
) | |||
) | |||
), | |||
'submitbtn' => array( | |||
'type' => 'submit', | |||
'value' => "Submit" // get_string() | |||
) | |||
) | |||
); | |||
function testform_validate(Pieform $form, $values) { | |||
if (isset($values['lastname']) && $values['firstname'] == 'Joe' && $values['lastname'] == 'Schmoe') { | |||
$form->set_error('lastname', "That's not your real name! That was the default."); // get_string() | |||
} | |||
} | |||
function testform_submit(Pieform $form, $values) { | |||
global $SESSION; | |||
// Normally here you would save the submitted values into the database | |||
// If you set this up as a JS form, use json_reply() | |||
if (!empty($values['lastname'])) { | |||
$name = "{$values['firstname']} {$values['lastname']}"; | |||
} else { | |||
$name = $values['firstname']; | |||
} | |||
$SESSION->add_ok_msg("Kia ora, {$name}!"); | |||
redirect(get_config('wwwroot') . 'test.php'); | |||
} | |||
// When you call pieform(), the Pieforms library does a bunch of automagical stuff. | |||
// It checks whether the form was submitted, and if so calls the form's validate | |||
// and/or submit callback functions, which usually results in a call to redirect(), ending | |||
// the page load. | |||
// If the form wasn't yet submitted, then pieform() renders the form into the necessary | |||
// HTML to print it on the page. | |||
// IMPORTANT: all pieform rendering needs to happen before calling smarty() for javascript | |||
// to be added to the page correctly | |||
$formhtml = pieform($form); | |||
// Instantiate the smarty object. | |||
$smarty = smarty(); | |||
// Variables you assign to the smarty become accessible in the template | |||
$smarty->assign('hard_coded', $hardcoded); | |||
$smarty->assign('user_raw', $userraw); | |||
$smarty->assign('form', $formhtml); | |||
// Specify the template file path, relative to htdocs/theme/raw/templates/ | |||
$smarty->display('test.tpl'); | |||
</source> | |||
htdocs/theme/raw/templates/test.tplのコンテンツは以下のようになります: | |||
<source lang="smarty" enclose="div"> | |||
{include file="header.tpl"} | |||
<!-- Stuff in here is HTML except for the bits in curly braces --> | |||
<!-- All variables printed here are run through an HTML escaping process, unless | |||
they have the "|safe" tag. --> | |||
{$hard_coded|safe} | |||
<!-- Uncleaned user data shouldn't be considered safe --> | |||
{$user_raw} | |||
<!-- Any function in the calling PHP file's namespace can be invoked by adding | |||
it after a "|" in the curly brackets --> | |||
{$hard_coded|strtoupper} | |||
<!-- The output from pieform() is the HTML output of the form. So you can just print it. --> | |||
{$form|safe} | |||
{include file="footer.tpl"} | |||
</source> |
Latest revision as of 22:00, 15 March 2016
以下、すべての新しいPHPファイル用のテンプレートです。あなたのファイルに外部からアクセスできるか否かにより、異なるテンプレートがあります。
このプロジェクトでは、APIドキュメントにPHPDocsを使用します。また、このプロジェクトはGPLとして許諾され、結果としてヘッダには特定の文言が含まれます (あなたのプログラムがGPLとして許諾されるためには、この文言が必要です。詳細はhttp://www.gnu.org/copyleft/gpl.htmlをご覧ください)。
訪問者がアクセスすると考えられるファイルのテンプレート
. /** * Mahara: Electronic portfolio, weblog, resume builder and social networking * Copyright (C) 2011 Copyright Holder * * (rest of the GPL statement) * * @package mahara * @subpackage core or plugintype/pluginname * @author Firstname Lastname */ define('INTERNAL', 1); require('init.php'); // ここにあなたのコードを記述します :)
あなたが訪問者にアクセスして欲しくないファイルのテンプレート
. /** * Mahara: Electronic portfolio, weblog, resume builder and social networking * Copyright (C) 2011 Copyright Holder * * (rest of the GPL statement) * * @package mahara * @subpackage core or plugintype/pluginname * @author Firstname Lastname */ defined('INTERNAL') || die(); // ここにあなたのコードを記述します :)
メモ:
- php閉じタグ "?>" は入れないでください。
- 勤務時間にプロジェクトの作業を実施した場合、Catalyst社員は著作権表示 "Catalyst IT Ltd" を追加してください。
- 作者 (author) フィールドには、会社ではなく、常に人 (1名または複数名) を指定してください。
pieformsおよびdwooを使用したサンプルページ
<?php
define('INTERNAL', 1);
require('init.php');
require_once('pieforms/pieform.php');
// Get data from the user by using the param_ functions, which will whitelist
// the input
$userint = param_integer('int',0);
// If none of the whitelisting options is acceptable, you can use param_variable,
// but be careful not to print the results to the screen without cleaning them
// first. That's a XSS vulnerability.
$userraw = param_variable('raw', 'no user input provided');
// Generate page content
// NOTE: Normally you should put hard-coded strings into a lang file under /lang, so that
// they can be internationalized with get_string()
$hardcoded = "This is a hard-coded string";
// See https://wiki.mahara.org/index.php/Developer_Area/Core_Subsystems/Form_API_%28Pieforms%29
// Normally you'd populate the default values with values from the DB
$form = array(
'name' => 'testform',
'method' => 'post',
'action' => "", // self
'successcallback' => 'testform_submit', // defaults to name_submit
'validatecallback' => 'testform_validate', // defaults to name_validate
'elements' => array(
'fullname' => array(
'type' => 'fieldset',
'legend' => "Your name", // should use get_string()...
'elements' => array(
'firstname' => array(
'type' => 'text',
'title' => "First name", // should use get_string()
'description' => "Your first, or given name", // get_string()
'help' => "Surely you know your own first name.", // get_string()
'defaultvalue' => 'Joe',
'rules' => array(
'required' => true
)
),
'lastname' => array(
'type' => 'text',
'title' => "Last name", // get_string()
'defaultvalue' => 'Schmoe',
'description' => "Your last, or family name", // get_string()
)
)
),
'submitbtn' => array(
'type' => 'submit',
'value' => "Submit" // get_string()
)
)
);
function testform_validate(Pieform $form, $values) {
if (isset($values['lastname']) && $values['firstname'] == 'Joe' && $values['lastname'] == 'Schmoe') {
$form->set_error('lastname', "That's not your real name! That was the default."); // get_string()
}
}
function testform_submit(Pieform $form, $values) {
global $SESSION;
// Normally here you would save the submitted values into the database
// If you set this up as a JS form, use json_reply()
if (!empty($values['lastname'])) {
$name = "{$values['firstname']} {$values['lastname']}";
} else {
$name = $values['firstname'];
}
$SESSION->add_ok_msg("Kia ora, {$name}!");
redirect(get_config('wwwroot') . 'test.php');
}
// When you call pieform(), the Pieforms library does a bunch of automagical stuff.
// It checks whether the form was submitted, and if so calls the form's validate
// and/or submit callback functions, which usually results in a call to redirect(), ending
// the page load.
// If the form wasn't yet submitted, then pieform() renders the form into the necessary
// HTML to print it on the page.
// IMPORTANT: all pieform rendering needs to happen before calling smarty() for javascript
// to be added to the page correctly
$formhtml = pieform($form);
// Instantiate the smarty object.
$smarty = smarty();
// Variables you assign to the smarty become accessible in the template
$smarty->assign('hard_coded', $hardcoded);
$smarty->assign('user_raw', $userraw);
$smarty->assign('form', $formhtml);
// Specify the template file path, relative to htdocs/theme/raw/templates/
$smarty->display('test.tpl');
htdocs/theme/raw/templates/test.tplのコンテンツは以下のようになります:
{include file="header.tpl"}
<!-- Stuff in here is HTML except for the bits in curly braces -->
<!-- All variables printed here are run through an HTML escaping process, unless
they have the "|safe" tag. -->
{$hard_coded|safe}
<!-- Uncleaned user data shouldn't be considered safe -->
{$user_raw}
<!-- Any function in the calling PHP file's namespace can be invoked by adding
it after a "|" in the curly brackets -->
{$hard_coded|strtoupper}
<!-- The output from pieform() is the HTML output of the form. So you can just print it. -->
{$form|safe}
{include file="footer.tpl"}