質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Smarty

Smartyは、PHPアプリケーションで使用されるテンプレートエンジンです。

Q&A

解決済

2回答

1995閲覧

【PHP・Smarty?】PHPファイルから tplファイルへ値を渡したい

azuapricot

総合スコア2341

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Smarty

Smartyは、PHPアプリケーションで使用されるテンプレートエンジンです。

0グッド

0クリップ

投稿2019/08/22 07:38

現在『 booked 』 という予約管理システムのオープンソースを改修しています。

booked は 画面表示に tplファイルを使うらしく、
試しに画面に1項目増やしてみているのですが、値が表示されません。

以下該当コードとなります。

データ格納部分

PHP

1<?php 2 3require_once(ROOT_DIR . 'Pages/Reservation/ReservationPage.php'); 4 5interface IExistingReservationPage extends IReservationPage 6{ 7 8// 省略 9 10 /** 11 * @param $test string 12 */ 13 // 追加部分 14 function SetTest($test); 15} 16 17class ExistingReservationPage extends ReservationPage implements IExistingReservationPage 18{ 19 protected $IsEditable = false; 20 protected $IsApprovable = false; 21 22 public function __construct() 23 { 24 parent::__construct(); 25 } 26 27 public function PageLoad() 28 { 29 parent::PageLoad(); 30 } 31 32 protected function GetPresenter() 33 { 34 $preconditionService = new EditReservationPreconditionService(); 35 $reservationViewRepository = new ReservationViewRepository(); 36 37 return new EditReservationPresenter($this, 38 $this->LoadInitializerFactory(), 39 $preconditionService, 40 $reservationViewRepository); 41 } 42 43 protected function GetTemplateName() 44 { 45 $readOnly = $this->GetQuerystring(QueryStringKeys::READ_ONLY) == 1; 46 47 if (!$readOnly && $this->IsApprovable && !$this->UpdatingBeforeApproving()) 48 { 49 return 'Reservation/approve.tpl'; 50 } 51 if (!$readOnly && $this->IsEditable) 52 { 53 return 'Reservation/edit.tpl'; 54 } 55 return 'Reservation/view.tpl'; 56 } 57 58 protected function GetReservationAction() 59 { 60 return ReservationAction::Update; 61 } 62 63 public function SetAdditionalResources($additionalResourceIds) 64 { 65 $this->Set('AdditionalResourceIds', $additionalResourceIds); 66 } 67 68 // 試しに追加してみたところ 69 public function SetTest($test) 70 { 71 $this->Set('Testtest', "Testtest"); 72 } 73} 74 75class DuplicateReservationPage extends ExistingReservationPage 76{ 77 public function __construct() 78 { 79 parent::__construct(); 80 $this->Set('Title', Resources::GetInstance()->GetString('DuplicateReservation')); 81 } 82 83 protected function GetTemplateName() 84 { 85 return 'Reservation/create.tpl'; 86 } 87 88 public function PageLoad() 89 { 90 parent::PageLoad(); 91 92 $this->Set('ReturnUrl', urldecode($this->GetQuerystring('return'))); 93 } 94 95 protected function GetReservationAction() 96 { 97 return ReservationAction::Create; 98 } 99 100 protected function GetReturnUrl() 101 { 102 return urldecode($this->GetQuerystring(QueryStringKeys::REDIRECT)); 103 } 104 105 public function SetTermsAccepted($accepted) 106 { 107 $this->Set('TermsAccepted', false); 108 } 109}

継承元ファイル(特に変更なし)

PHP

1<?php 2 3require_once(ROOT_DIR . 'Pages/SecurePage.php'); 4require_once(ROOT_DIR . 'Presenters/Reservation/ReservationPresenter.php'); 5 6interface IReservationPage extends IPage 7{ 8 /** 9 * @param $startPeriods array|SchedulePeriod[] 10 * @param $endPeriods array|SchedulePeriod[] 11 * @parma $lockDates bool 12 */ 13 public function BindPeriods($startPeriods, $endPeriods, $lockPeriods); 14 15 /** 16 * @param $resources array|ResourceDto[] 17 */ 18 public function BindAvailableResources($resources); 19 20 /** 21 * @param $accessories Accessory[] 22 */ 23 public function BindAvailableAccessories($accessories); 24 25 /** 26 * @param IResource $resource 27 */ 28 public function SetReservationResource($resource); 29 30 /** 31 * @param ReservationUserView[] $participants 32 */ 33 public function SetParticipants($participants); 34 35 /** 36 * @param ReservationUserView[] $invitees 37 */ 38 public function SetInvitees($invitees); 39 40 /** 41 * @param $accessories ReservationAccessory[]|array 42 */ 43 public function SetAccessories($accessories); 44 45 /** 46 * @param $attachments ReservationAttachmentView[]|array 47 */ 48 public function SetAttachments($attachments); 49 50 /** 51 * @param bool $canShowAdditionalResources 52 */ 53 public function ShowAdditionalResources($canShowAdditionalResources); 54 55 /** 56 * @param TermsOfService $termsOfService 57 */ 58 public function SetTerms($termsOfService); 59 60 /** 61 * @param bool $accepted 62 */ 63 public function SetTermsAccepted($accepted); 64} 65 66abstract class ReservationPage extends Page implements IReservationPage 67{ 68 protected $presenter; 69 protected $available = true; 70 71 /** 72 * @var PermissionServiceFactory 73 */ 74 protected $permissionServiceFactory; 75 76 public function __construct($title = null) 77 { 78 parent::__construct($title); 79 80 if (is_null($this->permissionServiceFactory)) 81 { 82 $this->permissionServiceFactory = new PermissionServiceFactory(); 83 } 84 } 85 86 /** 87 * @return IReservationPresenter 88 */ 89 protected abstract function GetPresenter(); 90 91 /** 92 * @return string 93 */ 94 protected abstract function GetTemplateName(); 95 96 /** 97 * @return string 98 */ 99 protected abstract function GetReservationAction(); 100 101 public function PageLoad() 102 { 103 $this->presenter = $this->GetPresenter(); 104 $this->presenter->PageLoad(); 105 106 $this->Set('ReturnUrl', $this->GetReturnUrl()); 107 $this->Set('ReservationAction', $this->GetReservationAction()); 108 $this->Set('MaxUploadSize', UploadedFile::GetMaxSize()); 109 $this->Set('MaxUploadCount', UploadedFile::GetMaxUploadCount()); 110 $this->Set('RepeatEveryOptions', range(1, 20)); 111 112 if ($this->IsUnavailable()) 113 { 114 $this->RedirectToError(ErrorMessages::RESERVATION_NOT_AVAILABLE); 115 return; 116 } 117 118 $this->Display($this->GetTemplateName()); 119 } 120 121 public function BindAvailableResources($resources) 122 { 123 $this->Set('AvailableResources', $resources); 124 } 125 126 public function SetReservationResource($resource) 127 { 128 $this->Set('ResourceName', $resource->GetName()); 129 $this->Set('ResourceId', $resource->GetId()); 130 $this->Set('Resource', $resource); 131 } 132 133 public function ShowReservationDetails($showReservationDetails) 134 { 135 $this->Set('ShowReservationDetails', $showReservationDetails); 136 } 137 138 public function HideRecurrence($isHidden) 139 { 140 $this->Set('HideRecurrence', $isHidden); 141 } 142 143 protected function GetReturnUrl() 144 { 145 $redirect = $this->GetQuerystring(QueryStringKeys::REDIRECT); 146 if (!empty($redirect)) 147 { 148 return $redirect; 149 } 150 return $this->GetLastPage(Pages::SCHEDULE); 151 } 152 153 protected function LoadInitializerFactory() 154 { 155 $userRepository = new UserRepository(); 156 return new ReservationInitializerFactory( 157 new ScheduleRepository(), $userRepository, new ResourceService(new ResourceRepository(), 158 $this->permissionServiceFactory->GetPermissionService(), 159 new AttributeService(new AttributeRepository()), 160 $userRepository, 161 new AccessoryRepository()), 162 new ReservationAuthorization(AuthorizationServiceFactory::GetAuthorizationService()) 163 ); 164 } 165 166 public function SetAvailability(DateRange $availability) 167 { 168 $this->Set('AvailabilityStart', $availability->GetBegin()); 169 $this->Set('AvailabilityEnd', $availability->GetEnd()); 170 } 171 172 public function SetTerms($termsOfService) 173 { 174 $this->Set('Terms', $termsOfService); 175 } 176}

create.tpl (画面テンプレート)

ダメ元で追加してみた(もちろん表示されなかった)

該当する tpl_cファイル を削除して再生成しても同様。。。

php

1<p> 2 { $Testtest } 3</p>

画面出力確認結果

HTML

1<p> 2 3</p>

↑ pタグは出てるけどセットしたはずの Testtest は出力されなかった


ざっくりしすぎてるのでダメ元で質問させていただいてます。。。

smartyとやらも使われてるらしく、どこかに記述が足りないんじゃないかとは思うのですが中々見つけられず・・・。

お時間ある方で、tplファイルにPHPからデータ渡したことある方や、

お気づきの点ある方いらっしゃいましたらご助力頂けますと幸いです。。。

足りない情報等あればお申し付け下さい。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2019/08/22 07:46

通常はPHP側からassign()してview読み込みしたら使えるんですけど、 $this->Set()の挙動次第ですね。 私も初めて聞いたOSSなので実際に入れてみて確認するしかなさそう。 無償版でも再現できますかね?
m.ts10806

2019/08/22 07:47

おぉ・・30日間試用版しかないのか。
azuapricot

2019/08/22 07:49

(導入したの私じゃないのでどうなんでしょう・・・!) ただ、booked インストールの仕方は↓に一応のせておきます https://qiita.com/ykoji1112/items/14995071ff991ec7e533 (ダメ元で質問させて頂いてるので回答する質問がなくなったときにで大丈夫です。。。笑)
azuapricot

2019/08/22 07:50

とりあえずassign()してる場所探して眺めてみます。。。
m.ts10806

2019/08/22 07:53

たぶんSet()がラッパーっぽいのでそこにあるとは思います。 あとは呼び出せてるかどうかかなと。
m.ts10806

2019/08/22 08:15

>https://qiita.com/ykoji1112/items/14995071ff991ec7e533 半分以上XAMPP導入ですね・・ ちょっと見てるところが違ったみたいで、ダウンロードできました。 私が見てたところは有償版のトライアルっぽいです。 勘で回答してしまってますが、私の方も導入してみてみます
azuapricot

2019/08/22 08:16

ReservationComponentBinder.php っていうファイルのところに、 /** * @var IExistingReservationPage */ private $page; $this->page->SetTitle($this->reservationView->Title); っていう記述が・・・ ここに・・・かいて・・・あげれば・・・もしかしたら・・・・ためしてみます・・・・・
azuapricot

2019/08/22 08:21

ウワアアアアアアアアアアアアアアア!表示されたああああああああ!ヤッターーーーー!!!!!!!
m.ts10806

2019/08/22 08:23

よかったよかった ただ私の適当回答をベストアンサーにするのは・ちょっと、、、
azuapricot

2019/08/22 08:27

いえ、呼び出されてないって気づいてなかったので・・・ 呼び出し部分を探したら万事解決もーまんたいでした。 ナイスな助言でした!!◎◎◎
m.ts10806

2019/08/22 08:29

いえいえ。コード内に呼びだしがないなぁって思っただけなので。 もしかしたら親の親の親の・・基盤のほうで「定義したら勝手に呼びだしてる機能がある」可能性もあったので、探ってみる必要はあるかなと思って、ちょうどダウンロードしてコードだけ見てるところでした。
azuapricot

2019/08/22 08:33

ダウンロードまでして頂きありがとうございました! 私は無能だったので「function SetTest($test);」が呼び出し部分だとめちゃくちゃ勘違いしてました()
azuapricot

2019/08/22 08:40

うわぁ・・・ほんとですね・・・・ interface 実装してるだけじゃん・・・もっとちゃんと見ておくべきだった(無能) 理解が深まりました・・・解決後もありがとうございます(感涙)
m.ts10806

2019/08/22 08:49

困ったときのPHPマニュアルですね。
guest

回答2

0

一応記述が足りなかった場所を書いておきます

ReservationComponentBinder.php

booked > lib > Application > Reservation > ReservationComponentBinder.php

(皆さんの階層がこれと同じかはわかりませんが・・・)

PHP

1class ReservationDetailsBinder implements IReservationComponentBinder 2{ 3 public function Bind(IReservationComponentInitializer $initializer) 4 { 5 // ここが呼び出し部分じゃった 6 $this->page->SetTest($this->reservationView->Title); 7 8}

これを足したら・・・

イメージ説明

2日かかった・・・m.ts10806さんありがとうございました(感涙)

投稿2019/08/22 08:26

azuapricot

総合スコア2341

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

今のコード見た限りの勘で答えてみます。(外してたらすみません)
SetTest()自体は今のところ実装だけでどこからも呼び出されてないように見えます。
Smartyはview呼び出しの前までにassignされた変数がview側で使えるようになるはずなので、
例えば・・・・
$this->Display($this->GetTemplateName());
この直前で呼び出すとどうなりますか?

投稿2019/08/22 07:52

m.ts10806

総合スコア80850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

azuapricot

2019/08/22 08:22

うわ~~~~~~ m.ts10806さんのおかげで表示されましたヤッタアアアアアア! 勘のとおりで、呼び出していたつもりで呼び出していませんでした・・・・ ナイスなヒントありがとうございます!!!!! 助かりました~~~~~~~!!!!
m.ts10806

2019/08/22 08:24

結構入り組んでる感じですね。 むかしCMS(XOOPS)の改修とかやったことあるんですけど、 あっちこっちいってて分かりづらかった記憶があります。そういえばXOOPSもviewはSmartyですね。 解決されたようで何よりです
azuapricot

2019/08/22 08:29

PHP案件はいくつかやったことがありますがここまで入り組んでるやつは初めてでした・・・
m.ts10806

2019/08/22 08:30

CMSは結構えげつないですよ。 IDEないと追うの無理です
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問