@@ -223,29 +223,22 @@ public function withView(string $file, array $args = array()): self
223223 */
224224 public function setPartial (string $ partial , array $ args = array ()): self
225225 {
226-
227- $ file = $ key2 = $ key = $ partial ;
228-
229- $ partial = explode (". " , $ file );
230- if (count ($ partial ) > 1 ) {
231- $ key2 = $ file = $ partial [1 ];
232- $ key = $ partial [0 ];
233-
234- if (isset ($ partial [2 ])) {
235- $ key2 = "{$ key }- {$ partial [2 ]}" ;
236-
237- }
238- }
239-
226+ $ keys = $ this ->selectPartial ($ partial , $ file );
240227 if (is_null ($ this ->file )) {
241228 $ this ->setFile ($ file );
242229 }
243230 $ func = $ this ->build ($ this ->file , $ args );
244- $ this ->partial [$ key ][ $ key2 ] = $ func ;
231+ $ this ->partial [$ keys [ 0 ]][ $ keys [ 1 ] ] = $ func ;
245232
246233 return $ this ;
247234 }
248235
236+ public function hasPartial ($ partial ): bool
237+ {
238+ $ keys = $ this ->selectPartial ($ partial );
239+ return isset ($ this ->partial [$ keys [0 ]][$ keys [1 ]]);
240+ }
241+
249242 /**
250243 * Unset a setted partial
251244 * @param string $key Partal key, example: ("sidebar", "breadcrumb")
@@ -406,10 +399,8 @@ private function buildView(): void
406399 */
407400 private function build (string |callable $ file , array $ args = array ()): callable
408401 {
409-
410402 $ this ->arguments = $ args ;
411403 $ func = function ($ argsFromFile ) use ($ file , $ args ) {
412-
413404 if (($ dir = ($ this ->dir [$ this ->get ] ?? null )) || !is_null ($ dir )) {
414405 if (is_callable ($ file )) {
415406 $ out = $ file ($ this , $ args );
@@ -523,4 +514,44 @@ public function isEl($elem): bool
523514 {
524515 return ($ elem instanceof Element);
525516 }
517+
518+
519+ /**
520+ * Get partial keys
521+ * @param string $partialKey
522+ * @param string|null &$file
523+ * @return array
524+ */
525+ final protected function selectPartial (string $ partialKey , ?string &$ file = null ): array
526+ {
527+ $ key = $ partialKey ;
528+ $ partial = explode (". " , $ partialKey );
529+ $ file = $ key2 = $ key = $ this ->cleanKey ($ key );
530+
531+ if (count ($ partial ) > 1 ) {
532+ $ key2 = $ partial [1 ];
533+ $ file = $ key2 = $ this ->cleanKey ($ key2 );
534+ $ key = $ partial [0 ];
535+ if (isset ($ partial [2 ])) {
536+ $ key2 = "{$ key }- {$ partial [2 ]}" ;
537+ }
538+ if (($ pos = strpos ($ key2 , "| " )) !== false ) {
539+ $ key2 = substr ($ key2 , 0 , $ pos );
540+ }
541+ }
542+ return [$ key , $ key2 ];
543+ }
544+
545+ /**
546+ * Clean key
547+ * @param string $key
548+ * @return string
549+ */
550+ final protected function cleanKey (string $ key ): string
551+ {
552+ if (strpos ($ key , "! " ) === 0 ) {
553+ return substr ($ key , 1 );
554+ }
555+ return $ key ;
556+ }
526557}
0 commit comments