search
HomeBackend DevelopmentPHP Tutorialxml在joomla表单中的应用详解分享_PHP

使用起来基本都有固定格式,在此不多说,这里主要说说xml在创建表单中的应用.表单的作用不言而喻,在模块配置里面有表单,组件里面也有表单,这里的配置里面的参数设置就是xml生成的表单,相对于通过直接建表单方便不少,这里通过编辑文章表单的使用过程来说,在使用之前,我们先来了解一下表单元素的类型,joomla提供了生成多种表单元素样式.
复制代码 代码如下:
//生成日历时间
//得到单元列表
//其他组件单元列表
//编辑器选择列表
//文件列表 html表示显示扩展名为html
//文件夹列表
//隐藏域






//密码
//单选



//单元选择
//水平线
//SQL生成列表
//文本框
//文本域无样式
//文本域有样式
//生成时区列表

上边就是提供的一些生成元素,但是如何使用呢,我们首先建立一个form.xml,在组件的models文件夹内
复制代码 代码如下:


表单测试






这里只有一个隐藏元素其实我们可以建立更多的元素,就是上边的那些生成元素类型,当然还可以自定义,这里的addpath就是自定义元素类型,位置就是在组建的elements文件夹内,上边的是内置的,笔者在这里定义了如下类型
复制代码 代码如下:
//自定义控件
//数组转换成列表 不写下标从0开始
//生成是否单选
//上传控件
//数组复选
//无限分类 注意添加section父类为0

使用方法如上,元素的自定义文件在下载里面提供.接着我们在views文件夹下面你的视图文件夹内的view.html.php增加如下语句
复制代码 代码如下:
$form = new JParameter('',JPATH_COMPONENT.DS.'models'.DS.'form.xml');
$form->set('m1','默认值');
$html=$form->render('details', 'html'); //details是元素名数组
$this->assignRef('html',$html);

上边第二句就是赋值,把隐藏域赋值,相当于编辑里面取值,第三四句是把表单html输出到模版,details是元素名称数组名,生成的m1的name就是details[m1],接着就是form.php调用如下
复制代码 代码如下:

详情
echo $this->html;
?>




这样一个表单就生成了,相对于直接写表单元素,这种方法更容易修改风格或者内容,修改只需要改xml文件即可,在joomla2.5版本中,基本上都是通过这种方式实现的,不过变化比较大,xml文件写法大不相同,通过xml是可以调用其他组件里面表单元素的.

表单元素的这些类型在模版配置参数,模块参数都是可以使用的,特别是可以自己扩展表单元素的样式,你完全可以定义出其他样式类型,笔者定义了比较常用的几种但是内置没有提供的,比如数组转换列表,数组转换复选,无限分类等样式.这里的类型其实就是api的JElement类,里面的源文件可以参考libraries\joomla\html\parameter\element,这里自定义表单元素怎么写呢这里给个例子或者参考源文件就明白了,文件名custom.php,类型也就是custom了,调用方法
复制代码 代码如下:
// 自定义显示
//
defined('_JEXEC') or die('Restricted access');

class JElementCustom extends JElement{
var $_name = 'Custom';
function fetchElement($name, $value, &$node, $control_name) {
$html=自定义
EOF;
return $html;
}
}

好了,就不多讲了,其实这只是一种建立表单的方法,如果你不喜欢,完全可以使用写表单元素,不过既然joomla提供了这些我们为什么不用呢?
下载自定义元素: elements_jb51.rar
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools